---
title: 'Vicarious Contact PSRM Replication: Cleaning'
author: "Lee-Or Ankori-Karlinsky, Rob Blair, Jessica Gottlieb, Samantha Moore-Berg"
output:
  pdf_document:
    toc: yes
    toc_depth: 3
  html_document:
    toc: yes
    toc_depth: '3'
    df_print: paged
editor_options:
  chunk_output_type: console
---

```{r setup, include=FALSE}
#set Markdown Root directory / working directory 


####Set Appropriate Working Directory####
knitr::opts_knit$set(root.dir= "~/Brown Dropbox/Lee-Or Ankori-Karlinsky/Braver Angels Documentary/PSRM_ReplicationPackage/ContentContact_ReplicationPackage",echo = TRUE) 
setwd("~/Brown Dropbox/Lee-Or Ankori-Karlinsky/Braver Angels Documentary/PSRM_ReplicationPackage/ContentContact_ReplicationPackage") 

library(tidyverse)
library(haven)
library(dotwhisker)
library(ggpubr)
library(margins)
library(lmtest)
library(sandwich)
library(psych)
library(performance)
library(corrr)
library(ipw)
library(WeightIt)
library(coefplot)
library(usdata)
library(readxl)
library(kableExtra)
library(magrittr)
library(mice)
library(rlang)
library(estimatr)
library(texreg)
library(marginaleffects)
library(janitor)
library(fastDummies)
library(xtable)
library(patchwork)
library(hms)


#load YouGov data
final_df <- read_csv("Data/OPRE0001_w1_w2_w3.csv")
final_df_codes <- read_csv("Data/OPRE0001_w1_w2_w3_codes.csv")

#load dropped cases (no need for now)
full_drops <- read_csv("Data/OPRE0001_w1_w2_w3_wdrops.csv")

#raw dropped cases
w1_drop <- read_csv("Data/DropRecords_w1.csv")
w2_drop <- read_csv("Data/DropRecords_w2.csv")
w3_drop <- read_csv("Data/DropRecords_w3.csv")

```


# Cleaning

```{r dropped cases wave 1}
#reasons given - quick look 
table(w1_drop$dropReason)
#Duplicate = 54 
#Open ends = 285
#Skipping = 25 
#Speeding = 30 


#create treatment assignment variables 
w1_drop <- w1_drop %>% 
  mutate(video_treatment = #video treatment
           case_when(
            video_treatment == 1 ~ "Treatment Long",
            video_treatment == 2 ~ "Treatment Short PMC",
            video_treatment == 3 ~ "Treatment Short VC",
            video_treatment == 4 ~ "Placebo Control",
            video_treatment == 5 ~ "Empty Control",
           ), 
         video_disp = 
          case_when(
             video_disp == 1 ~ "Full social proofing",
             video_disp == 2 ~ "Full meta",
             video_disp == 3 ~ "Full efficacy",
             video_disp == 4 ~ "Pmc social proofing",
             video_disp == 5 ~ "Pmc efficacy",
             video_disp == 6 ~ "Pmc meta",
             video_disp == 7 ~ "Vc social proofing",
             video_disp == 8 ~ "Vc efficacy",
             video_disp == 9 ~ "Vc meta",
             video_disp == 10 ~ "Control video"
           )
  )

#table(final_drops$dropReason_w3)
#table(w1_drop$video_treatment)

#check
table(is.na(w1_drop$video_treatment))
#38+72+79+71+80  #340 participants have assigned treatment: those who don't are duplicates.

#check nudges: 302 assigned nudge (difference due to empty control)
table(is.na(w1_drop$caseid))

#add removed in w1 dummy 
w1_drop <- w1_drop %>% 
  mutate(
    w1_dropped = 1
  )

#add wave 1 reason dummy 
w1_drop <- w1_drop %>% 
  mutate(
    w1_reason = dropReason
  )

#remove those without treatment assignment 
w1_drop <- w1_drop %>% 
  filter(!is.na(video_treatment))

table(w1_drop$dropReason)


```



```{r dropped cases wave 2 and 3}

#add removed in w2 dummy 
w2_drop <- w2_drop %>% 
  mutate(
    w2_dropped = if_else(dropReason != "Removed in wave 1", 1, 0)
  )

#check wave 2 only 
w2_drop <- w2_drop %>% 
  mutate(
    affpol_raw = 
      if_else(is.na(Q1_w2), 0, 1) |
      if_else(is.na(Q2_w2), 0, 1) |
      if_else(is.na(Q3_w2), 0, 1) |
      if_else(is.na(Q4_w2), 0, 1) |
      if_else(is.na(Q5_w2), 0, 1) |
      if_else(is.na(Q6_w2), 0, 1) |
      if_else(is.na(Q7_w2), 0, 1) |
      if_else(is.na(Q7_lean_w2), 0, 1)
  )

table(is.na(as.numeric(w2_drop$affpol_raw)))

w3_drop <- w3_drop %>% 
  mutate(
    w3_dropped = if_else(dropReason != "Removed in wave 1", 1,0)
  )

table(w3_drop$w3_dropped)

w3_drop <- w3_drop %>% 
  mutate(
    starttime_w3 = starttime,
    endtime_w3 = endtime
  )

#check wave 3 only 
w3_drop <- w3_drop %>% 
  mutate(
    affpol_raw = 
      if_else(is.na(as.numeric(Q1_w3)), 0, 1) |
      if_else(is.na(as.numeric(Q2_w3)), 0, 1) |
      if_else(is.na(as.numeric(Q3_w3)), 0, 1) |
      if_else(is.na(as.numeric(Q4_w3)), 0, 1) |
      if_else(is.na(as.numeric(Q5_w3)), 0, 1) |
      if_else(is.na(as.numeric(Q6_w3)), 0, 1) |
      if_else(is.na(as.numeric(Q7_w3)), 0, 1) |
      if_else(is.na(as.numeric(Q7_lean_w3)), 0, 1)
  )


table(is.na(w3_drop$affpol_raw))



```

```{r merge}
#column names
vars <- colnames(w1_drop)
vars

#modify column names for merge, caseid, drop reason, and demographics
newvars <- vars[c(1,2, 36:62)]

#merge wave 2 and wave 1 drops
tmp_drop <- left_join(w1_drop, w2_drop, by="caseid")

summary(tmp_drop$Q1_w2) #103 NAs
table(tmp_drop$Q1_w2) #47 non-values

103+47 #150
795-478 #still 317
table(is.na(tmp_drop$video_treatment))

check <- intersect(tmp_drop$caseid, w2_drop$caseid)
check2 <- intersect(w1_drop$caseid, w3_drop$caseid)



#remove those wihtout video treatment 
tmp_drop <- tmp_drop %>% 
  filter(!is.na(video_treatment))

#remove duplicates based on caseid, but prioritize non-NA values on affective polarization index to avoid data loss
tmp_drop <- tmp_drop %>% 
  arrange(desc(!is.na(as.numeric(Q1_w2))),
        #  desc(!is.na(as.numeric(Q3_w2))),
         # desc(!is.na(as.numeric(Q4_w2))),
        #  desc(!is.na(as.numeric(Q5_w2))),
         # desc(!is.na(as.numeric(Q6_w2))),
          #desc(!is.na(as.numeric(Q7_w2))),
          #desc(!is.na(as.numeric(Q7_lean_w2))
)%>% 
  distinct(caseid, .keep_all = T)

table(is.na(tmp_drop$caseid))

summary(tmp_drop$Q1_w2)
table(tmp_drop$Q1_w2)
#131 NAs
#56 
131+56
493-187
306

#find duplicate variables 
w1_w2_vars <- colnames(tmp_drop)

#create new variables
colnames(w3_drop)
merge_vars = w1_w2_vars[c(1,2, 111:137)]

#merge all three wave drops
all_drops <- left_join(tmp_drop,w3_drop, by="caseid")

table(is.na(all_drops$video_treatment))

#remove duplicates
all_drops <- all_drops %>% 
  distinct(caseid, .keep_all=T)

table((tmp_drop$Q1_w2))
summary(tmp_drop$Q1_w2)

#create dummy "bad case" variable 
all_drops <- all_drops %>% 
  mutate(
    bad_case = 1
  )

table(is.na(tmp_drop$Q1_w2))

#create dummy for aff_pol data 
all_drops <- all_drops %>% 
  mutate(
    affpol_raw = 
      if_else(is.na(Q1_w2), 0, 1) |
      if_else(is.na(Q2_w2), 0, 1) |
      if_else(is.na(Q3_w2), 0, 1) |
      if_else(is.na(Q4_w2), 0, 1) |
      if_else(is.na(Q5_w2), 0, 1) |
      if_else(is.na(Q6_w2), 0, 1) |
      if_else(is.na(Q7_w2), 0, 1) |
      if_else(is.na(Q7_lean_w2), 0, 1)
  )

#340 raw affpol scores of 641
table(all_drops$affpol_raw)


```


```{r prep final merge}
#modify variables to match prior to merging

#gender
all_drops <- all_drops %>% 
  mutate(
    gender = case_when(
      gender == 1 ~ "Male",
      gender == 2 ~ "Female"
    )
  )

#race 
all_drops <- all_drops %>% 
  mutate(
    race = case_when(
      race == 1 ~ "White",
      race == 2 ~ "Black",
      race == 3 ~ "Hispanic",
      race == 4 ~ "Asian",
      race == 5 ~ "Native American",
      race == 6 ~ "Two or more races",
      race == 7 ~ "Other",
      race == 8 ~ "Middle Eastern"
    )
  )

#education 
all_drops <- all_drops %>% 
  mutate(
    educ = case_when(
      educ == 1 ~ "No HS",
      educ == 2 ~ "High school graduate",
      educ == 3 ~ "Some college",
      educ == 4 ~ "2-year",
      educ == 5 ~ "4-year",
      educ == 6 ~ "Post-grad",
    )
  )

states <- state.name

states[5]

#state
all_drops <- all_drops %>% 
  mutate(
    inputstate = case_when(
      inputstate == 1 ~ states[1],
      inputstate == 2 ~ states[2],
      inputstate == 4 ~ states[3],
      inputstate == 5 ~ states[4], 
      inputstate == 6 ~ states[5], 
      inputstate == 8 ~ states[6],
      inputstate == 9 ~ states[7],
      inputstate == 10 ~ states[8], 
      inputstate == 11 ~ "District of Columbia",
      inputstate == 12 ~ states[9],
      inputstate == 13 ~ states[10],
      inputstate == 15 ~ states[11],
      inputstate == 16 ~ states[12],
      inputstate == 17 ~ states[13],
      inputstate == 18 ~ states[14],
      inputstate == 19 ~ states[15],
      inputstate == 20 ~ states[16],
      inputstate == 21 ~ states[17],
      inputstate == 22 ~ states[18],
      inputstate == 23 ~ states[19],
      inputstate == 24 ~ states[20],
      inputstate == 25 ~ states[21],
      inputstate == 26 ~ states[22],
      inputstate == 27 ~ states[23],
      inputstate == 28 ~ states[24],
      inputstate == 29 ~ states[25],
      inputstate == 30 ~ states[26],
      inputstate == 31 ~ states[27],
      inputstate == 32 ~ states[28],
      inputstate == 33 ~ states[29],
      inputstate == 34 ~ states[30],
      inputstate == 35 ~ states[31],
      inputstate == 36 ~ states[32],
      inputstate == 37 ~ states[33],
      inputstate == 38 ~ states[34],
      inputstate == 39 ~ states[35],
      inputstate == 40 ~ states[36],
      inputstate == 41 ~ states[37],
      inputstate == 42 ~ states[38],
      inputstate == 44 ~ states[39],
      inputstate == 45 ~ states[40],
      inputstate == 46 ~ states[41],
      inputstate == 47 ~ states[42],
      inputstate == 48 ~ states[43],
      inputstate == 49 ~ states[44],
      inputstate == 50 ~ states[45],
      inputstate == 51 ~ states[46],
      inputstate == 53 ~ states[47],
      inputstate == 54 ~ states[48],
      inputstate == 55 ~ states[49],
      inputstate == 56 ~ states[50]
  )
)

table(all_drops$inputstate)

#region
all_drops <- all_drops %>% 
  mutate(
    region = case_when(
      region == 1 ~ "Northeast",
      region == 2 ~ "Midwest",
      region == 3 ~ "South",
      region == 4 ~ "West"
    )
  )

#marstat 
all_drops <- all_drops %>% 
  mutate(
    marstat = case_when(
        marstat == 1 ~ "Married" ,
        marstat == 2 ~ "Separated", 
        marstat == 3 ~ "Divorced", 
        marstat == 4 ~ "Widowed", 
        marstat == 5 ~ "Never married", 
        marstat == 6 ~ "Domestic / civil partnership", 
        marstat == 8 ~ "skipped"
      )
    )

#child18
all_drops <- all_drops %>% 
  mutate(
    child18 = case_when(
        child18 == 1 ~ "Yes" ,
        child18 == 2 ~ "No", 
        child18 == 8 ~ "skipped"
      )
    )


#employ
all_drops <- all_drops %>% 
  mutate(
    employ = case_when(
        employ == 1 ~ "Full-time" ,
        employ == 2 ~ "Part-time", 
        employ == 3 ~ "Temporarily laid off",
        employ == 4 ~ "Unemployed",
        employ == 5 ~ "Retired",
        employ == 6 ~ "Permanently disabled",
        employ == 7 ~ "Homemaker", 
        employ == 8 ~ "Student", 
        employ == 9 ~ "Other",
        employ == 98 ~ "skipped"
      )
    )

#income
all_drops <- all_drops %>% 
  mutate(
    faminc_new = case_when(
        faminc_new == 1 ~ "Less than $10,000",
        faminc_new == 2 ~ "$10,000 - $19,000", 
        faminc_new == 3 ~ "$20,000 - $29,999",
        faminc_new == 4 ~ "$30,000 - $39,999",
        faminc_new == 5 ~ "$40,000 - $49,999",
        faminc_new == 6 ~ "$50,000 - $59,999",
        faminc_new == 7 ~ "$60,000 - $69,999", 
        faminc_new == 8 ~ "$70,000 - $79,999", 
        faminc_new == 9 ~ "$80,000 - $99,999",
        faminc_new == 10 ~ "$100,000 - $119,999",
        faminc_new == 11 ~ "$120,000 - $149,999",
        faminc_new == 12 ~ "$150,000 - $199,999",
        faminc_new == 13 ~ "$200,000 - $249,999",
        faminc_new == 14 ~ "$250,000 - $349,999", 
        faminc_new == 15 ~ "$350,000 - $499,999",
        faminc_new == 16 ~ "$500,000 or more",
        faminc_new == 97 ~ "Prefer not to say", 
        faminc_new == 998 ~ "skipped"
      )
    )

#voter registration
all_drops <- all_drops %>% 
  mutate(
    votereg = case_when(
      votereg == 1 ~ "Yes",
      votereg == 2 ~ "No",
      votereg == 3 ~ "Don't know",
      votereg == 8 ~ "skipped",
    )
  )

#turnout20post 
all_drops <- all_drops %>% 
  mutate(
    turnout20post = case_when(
      turnout20post == 1 ~ "Yes",
      turnout20post == 2 ~ "No",
      turnout20post == 8 ~ "skipped"
    )
  )

#presidential vote 2020
all_drops <- all_drops %>% 
  mutate(
    presvote20post = case_when(
      presvote20post == 1 ~ "Joe Biden",
      presvote20post == 2 ~ "Donald Trump",
      presvote20post == 3 ~ "Jo Jorgensen",
      presvote20post == 4 ~ "Howie Hawkins",
      presvote20post == 5 ~ "Other", 
      presvote20post == 6 ~ "Did not vote for President",
      presvote20post == 8 ~ "skipped"
    )
  )

#ideology
all_drops <- all_drops %>% 
  mutate(
    ideo5 = case_when(
      ideo5 == 1 ~ "Very liberal", 
      ideo5 == 2 ~ "Liberal",
      ideo5 == 3 ~ "Moderate",
      ideo5 == 4 ~ "Conservative",
      ideo5 == 5 ~ "Very conservative"
    )
  )

#news interest 
all_drops <- all_drops %>% 
  mutate(
    newsint = case_when(
      newsint == 1 ~ "Most of the time", 
      newsint == 2 ~ "Some of the time",
      newsint == 3 ~ "Only now and then",
      newsint == 4 ~ "Hardly at all",
      newsint == 7 ~ "Don't know",
      newsint == 98 ~ "skipped"
    )
  )

#presidential vote 2016
all_drops <- all_drops %>% 
  mutate(
    presvote16post = case_when(
      presvote16post == 1 ~ "Hillary Clinton",
      presvote16post == 2 ~ "Donald Trump",
      presvote16post == 3 ~ "Gary Johnson",
      presvote16post == 4 ~ "Jill Stein",
      presvote16post == 5 ~ "Evan McMullin", 
      presvote16post == 6 ~ "Other",
      presvote16post == 7 ~ "Did not vote for President",
      presvote16post == 98 ~ "skipped"
    )
  )

#born again
all_drops <- all_drops %>% 
  mutate(
    pew_bornagain = case_when(
      pew_bornagain == 1 ~ "Yes",
      pew_bornagain == 2 ~ "No",
      pew_bornagain == 8 ~ "skipped",
    )
  )


#importance of religion 
all_drops <- all_drops %>% 
  mutate(
    pew_religimp = case_when(
      pew_religimp == 1 ~ "Very important",
      pew_religimp == 2 ~ "Somewhat important",
      pew_religimp == 3 ~ "Not too important",
      pew_religimp == 4 ~ "Not at all important",
      pew_religimp == 8 ~ "skipped"
    )
  )

#pew prayer 
all_drops <- all_drops %>% 
  mutate(
    pew_prayer = case_when(
       pew_prayer == 1 ~ "Several times a day",
       pew_prayer == 2 ~ "Once a day",
       pew_prayer == 3 ~ "A few times a week",
       pew_prayer == 4 ~ "Once a week",
       pew_prayer == 5 ~ "A few times a month",
       pew_prayer == 6 ~ "Seldom",
       pew_prayer == 7 ~ "Never",
       pew_prayer == 8 ~ "Don't know",
       pew_prayer == 98 ~ "skipped"
    )
  )

#religion
all_drops <- all_drops %>% 
  mutate(
    religpew = case_when(
       religpew == 1 ~ "Protestant",
       religpew == 2 ~ "Roman Catholic",
       religpew == 3 ~ "Mormon",
       religpew == 4 ~ "Eastern or Greek Orthodox",
       religpew == 5 ~ "Jewish",
       religpew == 6 ~ "Muslim",
       religpew == 7 ~ "Buddhist",
       religpew == 8 ~ "Hindu",
       religpew == 9 ~ "Atheist",
       religpew == 10 ~ "Agnostic",
       religpew == 11 ~ "Nothing in particular",
       religpew == 12 ~ "Something else",
       religpew == 98 ~ "skipped"
    )
  )

#consent
all_drops <- all_drops %>% 
  mutate(
    consent = case_when(
      consent == 1 ~ "Yes", 
      consent == 2 ~ "No"
    )
  )

#consent 2 
all_drops <- all_drops %>% 
  mutate(
    consent2 = case_when(
      consent2 == 1 ~ "Proceed", 
      consent2 == 2 ~ "Do not want to participate"
    )
  )

#church attendance 
all_drops <- all_drops %>% 
  mutate(
    pew_churatd = case_when(
      pew_churatd == 1 ~ "More than once a week",
      pew_churatd == 2 ~ "Once a week",
      pew_churatd == 3 ~ "Once or twice a month",
      pew_churatd == 4 ~ "A few times a year",
      pew_churatd == 5 ~ "Seldom",
      pew_churatd == 6 ~ "Never",
      pew_churatd == 7 ~ "Don't know",
      pew_churatd == 98 ~ "skipped"
    )
  )


```

```{r prep final merge 2}
#modify variables to match prior to merging

#gender
full_drops <- full_drops %>% 
  mutate(
    gender = case_when(
      gender == 1 ~ "Male",
      gender == 2 ~ "Female"
    )
  )

#race 
full_drops <- full_drops %>% 
  mutate(
    race = case_when(
      race == 1 ~ "White",
      race == 2 ~ "Black",
      race == 3 ~ "Hispanic",
      race == 4 ~ "Asian",
      race == 5 ~ "Native American",
      race == 6 ~ "Two or more races",
      race == 7 ~ "Other",
      race == 8 ~ "Middle Eastern"
    )
  )

#education 
full_drops <- full_drops %>% 
  mutate(
    educ = case_when(
      educ == 1 ~ "No HS",
      educ == 2 ~ "High school graduate",
      educ == 3 ~ "Some college",
      educ == 4 ~ "2-year",
      educ == 5 ~ "4-year",
      educ == 6 ~ "Post-grad",
    )
  )

states <- state.name

states[5]

#state
full_drops <- full_drops %>% 
  mutate(
    inputstate = case_when(
      inputstate == 1 ~ states[1],
      inputstate == 2 ~ states[2],
      inputstate == 4 ~ states[3],
      inputstate == 5 ~ states[4], 
      inputstate == 6 ~ states[5], 
      inputstate == 8 ~ states[6],
      inputstate == 9 ~ states[7],
      inputstate == 10 ~ states[8], 
      inputstate == 11 ~ "District of Columbia",
      inputstate == 12 ~ states[9],
      inputstate == 13 ~ states[10],
      inputstate == 15 ~ states[11],
      inputstate == 16 ~ states[12],
      inputstate == 17 ~ states[13],
      inputstate == 18 ~ states[14],
      inputstate == 19 ~ states[15],
      inputstate == 20 ~ states[16],
      inputstate == 21 ~ states[17],
      inputstate == 22 ~ states[18],
      inputstate == 23 ~ states[19],
      inputstate == 24 ~ states[20],
      inputstate == 25 ~ states[21],
      inputstate == 26 ~ states[22],
      inputstate == 27 ~ states[23],
      inputstate == 28 ~ states[24],
      inputstate == 29 ~ states[25],
      inputstate == 30 ~ states[26],
      inputstate == 31 ~ states[27],
      inputstate == 32 ~ states[28],
      inputstate == 33 ~ states[29],
      inputstate == 34 ~ states[30],
      inputstate == 35 ~ states[31],
      inputstate == 36 ~ states[32],
      inputstate == 37 ~ states[33],
      inputstate == 38 ~ states[34],
      inputstate == 39 ~ states[35],
      inputstate == 40 ~ states[36],
      inputstate == 41 ~ states[37],
      inputstate == 42 ~ states[38],
      inputstate == 44 ~ states[39],
      inputstate == 45 ~ states[40],
      inputstate == 46 ~ states[41],
      inputstate == 47 ~ states[42],
      inputstate == 48 ~ states[43],
      inputstate == 49 ~ states[44],
      inputstate == 50 ~ states[45],
      inputstate == 51 ~ states[46],
      inputstate == 53 ~ states[47],
      inputstate == 54 ~ states[48],
      inputstate == 55 ~ states[49],
      inputstate == 56 ~ states[50]
  )
)

table(full_drops$inputstate)

#region
full_drops <- full_drops %>% 
  mutate(
    region = case_when(
      region == 1 ~ "Northeast",
      region == 2 ~ "Midwest",
      region == 3 ~ "South",
      region == 4 ~ "West"
    )
  )

#marstat 
full_drops <- full_drops %>% 
  mutate(
    marstat = case_when(
        marstat == 1 ~ "Married" ,
        marstat == 2 ~ "Separated", 
        marstat == 3 ~ "Divorced", 
        marstat == 4 ~ "Widowed", 
        marstat == 5 ~ "Never married", 
        marstat == 6 ~ "Domestic / civil partnership", 
        marstat == 8 ~ "skipped"
      )
    )

#child18
full_drops <- full_drops %>% 
  mutate(
    child18 = case_when(
        child18 == 1 ~ "Yes" ,
        child18 == 2 ~ "No", 
        child18 == 8 ~ "skipped"
      )
    )


#employ
full_drops <- full_drops %>% 
  mutate(
    employ = case_when(
        employ == 1 ~ "Full-time" ,
        employ == 2 ~ "Part-time", 
        employ == 3 ~ "Temporarily laid off",
        employ == 4 ~ "Unemployed",
        employ == 5 ~ "Retired",
        employ == 6 ~ "Permanently disabled",
        employ == 7 ~ "Homemaker", 
        employ == 8 ~ "Student", 
        employ == 9 ~ "Other",
        employ == 98 ~ "skipped"
      )
    )

#income
full_drops <- full_drops %>% 
  mutate(
    faminc_new = case_when(
        faminc_new == 1 ~ "Less than $10,000",
        faminc_new == 2 ~ "$10,000 - $19,000", 
        faminc_new == 3 ~ "$20,000 - $29,999",
        faminc_new == 4 ~ "$30,000 - $39,999",
        faminc_new == 5 ~ "$40,000 - $49,999",
        faminc_new == 6 ~ "$50,000 - $59,999",
        faminc_new == 7 ~ "$60,000 - $69,999", 
        faminc_new == 8 ~ "$70,000 - $79,999", 
        faminc_new == 9 ~ "$80,000 - $99,999",
        faminc_new == 10 ~ "$100,000 - $119,999",
        faminc_new == 11 ~ "$120,000 - $149,999",
        faminc_new == 12 ~ "$150,000 - $199,999",
        faminc_new == 13 ~ "$200,000 - $249,999",
        faminc_new == 14 ~ "$250,000 - $349,999", 
        faminc_new == 15 ~ "$350,000 - $499,999",
        faminc_new == 16 ~ "$500,000 or more",
        faminc_new == 97 ~ "Prefer not to say", 
        faminc_new == 998 ~ "skipped"
      )
    )

#voter registration
full_drops <- full_drops %>% 
  mutate(
    votereg = case_when(
      votereg == 1 ~ "Yes",
      votereg == 2 ~ "No",
      votereg == 3 ~ "Don't know",
      votereg == 8 ~ "skipped",
    )
  )

#turnout20post 
full_drops <- full_drops %>% 
  mutate(
    turnout20post = case_when(
      turnout20post == 1 ~ "Yes",
      turnout20post == 2 ~ "No",
      turnout20post == 8 ~ "skipped"
    )
  )

#presidential vote 2020
full_drops <- full_drops %>% 
  mutate(
    presvote20post = case_when(
      presvote20post == 1 ~ "Joe Biden",
      presvote20post == 2 ~ "Donald Trump",
      presvote20post == 3 ~ "Jo Jorgensen",
      presvote20post == 4 ~ "Howie Hawkins",
      presvote20post == 5 ~ "Other", 
      presvote20post == 6 ~ "Did not vote for President",
      presvote20post == 8 ~ "skipped"
    )
  )

#ideology
full_drops <- full_drops %>% 
  mutate(
    ideo5 = case_when(
      ideo5 == 1 ~ "Very liberal", 
      ideo5 == 2 ~ "Liberal",
      ideo5 == 3 ~ "Moderate",
      ideo5 == 4 ~ "Conservative",
      ideo5 == 5 ~ "Very conservative"
    )
  )

#news interest 
full_drops <- full_drops %>% 
  mutate(
    newsint = case_when(
      newsint == 1 ~ "Most of the time", 
      newsint == 2 ~ "Some of the time",
      newsint == 3 ~ "Only now and then",
      newsint == 4 ~ "Hardly at all",
      newsint == 7 ~ "Don't know",
      newsint == 98 ~ "skipped"
    )
  )

#presidential vote 2016
full_drops <- full_drops %>% 
  mutate(
    presvote16post = case_when(
      presvote16post == 1 ~ "Hillary Clinton",
      presvote16post == 2 ~ "Donald Trump",
      presvote16post == 3 ~ "Gary Johnson",
      presvote16post == 4 ~ "Jill Stein",
      presvote16post == 5 ~ "Evan McMullin", 
      presvote16post == 6 ~ "Other",
      presvote16post == 7 ~ "Did not vote for President",
      presvote16post == 98 ~ "skipped"
    )
  )

#born again
full_drops <- full_drops %>% 
  mutate(
    pew_bornagain = case_when(
      pew_bornagain == 1 ~ "Yes",
      pew_bornagain == 2 ~ "No",
      pew_bornagain == 8 ~ "skipped",
    )
  )


#importance of religion 
full_drops <- full_drops %>% 
  mutate(
    pew_religimp = case_when(
      pew_religimp == 1 ~ "Very important",
      pew_religimp == 2 ~ "Somewhat important",
      pew_religimp == 3 ~ "Not too important",
      pew_religimp == 4 ~ "Not at all important",
      pew_religimp == 8 ~ "skipped"
    )
  )

#pew prayer 
full_drops <- full_drops %>% 
  mutate(
    pew_prayer = case_when(
       pew_prayer == 1 ~ "Several times a day",
       pew_prayer == 2 ~ "Once a day",
       pew_prayer == 3 ~ "A few times a week",
       pew_prayer == 4 ~ "Once a week",
       pew_prayer == 5 ~ "A few times a month",
       pew_prayer == 6 ~ "Seldom",
       pew_prayer == 7 ~ "Never",
       pew_prayer == 8 ~ "Don't know",
       pew_prayer == 98 ~ "skipped"
    )
  )

#religion
full_drops <- full_drops %>% 
  mutate(
    religpew = case_when(
       religpew == 1 ~ "Protestant",
       religpew == 2 ~ "Roman Catholic",
       religpew == 3 ~ "Mormon",
       religpew == 4 ~ "Eastern or Greek Orthodox",
       religpew == 5 ~ "Jewish",
       religpew == 6 ~ "Muslim",
       religpew == 7 ~ "Buddhist",
       religpew == 8 ~ "Hindu",
       religpew == 9 ~ "Atheist",
       religpew == 10 ~ "Agnostic",
       religpew == 11 ~ "Nothing in particular",
       religpew == 12 ~ "Something else",
       religpew == 98 ~ "skipped"
    )
  )

#consent
full_drops <- full_drops %>% 
  mutate(
    consent = case_when(
      consent == 1 ~ "Yes", 
      consent == 2 ~ "No"
    )
  )

#consent 2 
full_drops <- full_drops %>% 
  mutate(
    consent2 = case_when(
      consent2 == 1 ~ "Proceed", 
      consent2 == 2 ~ "Do not want to participate"
    )
  )

#church attendance 
full_drops <- full_drops %>% 
  mutate(
    pew_churatd = case_when(
      pew_churatd == 1 ~ "More than once a week",
      pew_churatd == 2 ~ "Once a week",
      pew_churatd == 3 ~ "Once or twice a month",
      pew_churatd == 4 ~ "A few times a year",
      pew_churatd == 5 ~ "Seldom",
      pew_churatd == 6 ~ "Never",
      pew_churatd == 7 ~ "Don't know",
      pew_churatd == 98 ~ "skipped"
    )
  )


```


```{r final merge}

####Add drop report to clean dataset ####

#create variable merge list
final_vars <- colnames(final_df)
drop_vars <- colnames(all_drops)
shared_vars <- intersect(final_vars, drop_vars)

#turn variables to character
all_drops <- all_drops %>% 
  mutate_at(c(shared_vars[6:208]), as.character) #208 to return


final_df <- final_df %>% 
  mutate(
    starttime = as.character(starttime),
    endtime = as.character(endtime)
  )

#final_df <- final_df %>% 
#  mutate_at(c(shared_vars[6:208, 250]), as.character)

#merge
final_drops <- full_join(final_df, all_drops, by=c(shared_vars))

#remove duplicate case ids
final_drops <- final_drops %>% 
  distinct(caseid, .keep_all = T)

table(is.na(final_drops$video_treatment))

table(is.na(as.numeric(final_drops$Q2_w2)))

#keep only those assigned to treatment 
final_drops <- final_drops %>% 
  filter(!is.na(video_treatment))


```


## Sample Weights

```{r weights}

#Find out number of participants with weights for wave 3 but NOT wave 2 

#create variable
final_drops <- final_drops %>% 
  mutate(
    w3_only = if_else(weight_w2 =="No Data" & weight_w3 != "No Data", 1,0)
  )

#check
table(final_drops$w3_only)

#turn weights back to numeric 
final_drops$weight <- as.numeric(final_drops$weight)
final_drops$weight_w2 <- as.numeric(final_drops$weight_w2)
final_drops$weight_w3 <- as.numeric(final_drops$weight_w3)

```



## Treatment assignment indicators

```{r treatment assignment indicators}

#treatment assignment variable DROPS
final_drops <- final_drops %>% 
  mutate(treat_all =
           as.factor(case_when(
            video_treatment == "Empty Control" ~ 0, 
            video_treatment == "Placebo Control" ~ 1,
            video_treatment == "Treatment Short PMC" ~ 2,
            video_treatment == "Treatment Short VC" ~ 3, 
            video_treatment == "Treatment Long" ~ 4
           )
    ))


#check 
table(final_drops$treat_all)
table(final_drops$video_treatment)


#####Long#####


#create treatment long DROPS
final_drops <- final_drops %>% 
  mutate(treat_long =
           as.factor(case_when(
            video_treatment == "Empty Control" ~ 0, 
            video_treatment == "Placebo Control" ~ 1,
            video_treatment == "Treatment Long" ~ 2
           )
    ))

#check
table(final_drops$treat_long)




#treat long collapse 1 DROPS (Long video vs Placebo+Control)
final_drops <- final_drops %>% 
  mutate(treat_long_collapse1 =
           case_when(
            video_treatment == "Empty Control" ~ 0, 
            video_treatment == "Placebo Control" ~ 0,
            video_treatment == "Treatment Long" ~ 1
           )
    )

#check
table(final_drops$treat_long_collapse1)


#treat long collapse 2 DROPS (long vs. placebo)
final_drops <- final_drops %>% 
  mutate(treat_long_collapse2 =
           case_when(
            video_treatment == "Placebo Control" ~ 0,
            video_treatment == "Treatment Long" ~ 1
           )
    )

#check
table(final_drops$treat_long_collapse2)


#####Short#####

#Treat short variable DROPS (short pmc vs. short vc vs. empty control)
final_drops <- final_drops %>% 
  mutate(treat_short =
           as.factor(case_when(
            video_treatment == "Empty Control" ~ 0, 
            video_treatment == "Treatment Short PMC" ~ 1,
            video_treatment == "Treatment Short VC" ~ 2, 
           )
    ))

#check
table(final_drops$treat_short)


#Treat short collapse DROPS (short videos vs. control)
final_drops <- final_drops %>% 
  mutate(treat_short_collapse =
           case_when(
            video_treatment == "Empty Control" ~ 0, 
            video_treatment == "Treatment Short PMC" ~ 1,
            video_treatment == "Treatment Short VC" ~ 1, 
           )
    )

#check
table(final_drops$treat_short_collapse)


#pmc vs. vc
final_drops <- final_drops %>% #create dummy
  mutate(pmc_vs_vc =
           case_when(
            video_treatment == "Treatment Short VC" ~ 0,
            video_treatment == "Treatment Short PMC" ~ 1
           )
    )

#####LongShort#####

#Treat long short DROPS (long vs. short pmc vs. short vc vs. control)
final_drops <- final_drops %>% 
  mutate(treat_long_short =
           as.factor(case_when(
            video_treatment == "Empty Control" ~ 0, 
            video_treatment == "Treatment Short PMC" ~ 1,
            video_treatment == "Treatment Short VC" ~ 2, 
            video_treatment == "Treatment Long" ~ 3
           )
    ))

#check
table(final_drops$treat_long_short)


#Treat long short collapse DROPS (long vs. short pmc + vc vs. control) 
final_drops <- final_drops %>% 
  mutate(treat_long_short_collapse =
           as.factor(case_when(
            video_treatment == "Empty Control" ~ 0, 
            video_treatment == "Treatment Short PMC" ~ 1,
            video_treatment == "Treatment Short VC" ~ 1, 
            video_treatment == "Treatment Long" ~ 2
           )
    ))

#check
table(final_drops$treat_long_short_collapse)


#####ShortDummy#####


#a dummy for both short videos vs. control DROPS
final_drops <- final_drops %>% 
  mutate(
    short_pmc =
      case_when(
        video_treatment == "Treatment Short PMC" ~ 1,
        video_treatment == "Empty Control" ~ 0
    ), 
    short_vc = 
      case_when(
        video_treatment == "Treatment Short VC" ~ 1, 
        video_treatment == "Empty Control" ~ 0
      )
  )

table(is.na(final_drops$short_vc))


#Drops
#create dummies for short videos
#final_drops <- final_drops %>% 
#  mutate(
#    short_vc = if_else(video_treatment == 
#                         "Treatment Short VC", 1, 0),
#    short_pmc = if_else(video_treatment == 
#                          "Treatment Short PMC", 1, 0),
#    full_video = if_else(video_treatment == 
#                     "Treatment Long", 1, 0),
#    empty = if_else(video_treatment == 
#                        "Empty Control", 1, 0)
#  )

####Pooled####

#short vs. pooled controls
final_drops <- final_drops %>% 
  mutate(
    short_vs_pooled = 
      as.factor(case_when(
        video_treatment == "Placebo Control" ~ 0,
        video_treatment == "Empty Control" ~ 0,
        video_treatment == "Treatment Short PMC" ~ 1,
        video_treatment == "Treatment Short VC" ~ 2
      )
  ))

#all treatment vs. all control 

#drops
final_drops <- final_drops %>% 
  mutate(
    treat_all_collapse =
      case_when(
      video_treatment == "Treatment Long" ~ 1,
      video_treatment == "Treatment Short PMC" ~ 1,
      video_treatment == "Treatment Short VC" ~ 1, 
      video_treatment == "Empty Control" ~ 0,
      video_treatment == "Placebo Control" ~ 0
    )
  )


#both short videos vs. long video 
final_drops <- final_drops %>% 
  mutate(
    treat_shortlong_collapse =
      case_when(
      video_treatment == "Treatment Long" ~ 1,
      video_treatment == "Treatment Short PMC" ~ 0,
      video_treatment == "Treatment Short VC" ~ 0
    )
  )

#long vs. pmc / vc
final_drops <- final_drops %>% 
  mutate(
    treat_pmclong =
      case_when(
      video_treatment == "Treatment Long" ~ 1,
      video_treatment == "Treatment Short PMC" ~ 0
    ),
    treat_vclong = case_when(
      video_treatment == "Treatment Long" ~ 1,
      video_treatment == "Treatment Short VC" ~ 0
  )
  )

```


## Nudges treatment assignments

```{r nudges}

#create nudge dummies 
final_drops <- final_drops %>% 
  mutate(
    soc_proof = as.numeric(grepl("social proofing", final_drops$video_disp)),
    efficacy = as.numeric(grepl("efficacy", final_drops$video_disp)),
    meta = as.numeric(grepl("meta", final_drops$video_disp)),
  )

#create categorical variable (social proofing is the reference category, efficacy is 1, meta is 2)
final_drops <- final_drops %>% 
  mutate(nudges=
    as.factor(case_when(
      video_treatment == "Placebo Control" ~ 0,
      soc_proof == 1 & video_treatment == "Treatment Long" ~ 1,
      efficacy == 1 & video_treatment == "Treatment Long" ~ 2, 
      meta == 1 & video_treatment == "Treatment Long" ~ 3
    )
    ))


```


## Party ID indicators

```{r party ID}

# Party ID: Republican vs. Democrat DROPS
final_drops <- final_drops %>% 
  mutate(partyID =
           case_when(
            Q5 == "Republican" ~ 1, 
            Q5 == "1" ~ 1,
            Q6 == "Republican" ~ 1, 
            Q6 == "1" ~1,
            Q5 == "Democrat" ~ 0,
            Q5 == "2" ~ 0,
            Q6 == "Democrat" ~ 0, 
            Q6 == "2" ~ 0
           )
    )

#check
#table(final_drops$Q5) #826 Ds, 512 Rs (diff)
#table(final_drops$Q6) #488 Ds, 407 Rs (diff)
#table(final_drops$partyID) #1514 Ds, 1059 Rs 

#####Independents#####

#Party ID with Independents (others included with independents) DROPS
final_drops <- final_drops %>% 
  mutate(partyID_ind =
           as.factor(case_when(
            Q5 == "Republican" ~ 1, 
            Q5 == "1" ~ 1, #Republican
            Q5 == "Democrat" ~ 0,
            Q5 == "2" ~ 0, #Democrat
            Q5 == "Independent" ~ 2,
            Q5 == "3" ~ 2, #Ind
            Q5 == "Other" ~ 2,
            Q5 == "4" ~ 2 #Other
           )
    ))

#check
#table(final_drops$partyID_ind)
#955+588+1030


#Party ID with Ind and Other disaggregated DROPS
final_drops <- final_drops %>% 
  mutate(partyID_ind_other =
           as.factor(case_when(
            Q5 == "Republican" ~ 1, 
            Q5 == "1" ~ 1, #Republican
            Q5 == "Democrat" ~ 0,
            Q5 == "2" ~ 0, #Democrat,
            Q5 == "Independent" ~2,
            Q5 == "3" ~ 2, #Ind, 
            Q5 == "Other" ~ 3,
            Q5 == "4" ~ 3 #Other
           )
    ))

#check
#table(final_drops$partyID_ind_other)
#955+588+742+288

```

## Affective polarization index

```{r Feeling Thermometer}
#Feeling Thermometer 

#Inparty DROPS
final_drops <- final_drops %>% 
  mutate(therm_inparty =
           case_when(
            partyID == 0 ~ as.numeric(Q1_w2), #Dem Warmth toward Dem
            partyID == 1 ~ as.numeric(Q2_w2), #Rep Warmth towards Rep
           ),
         therm_inparty = replace(therm_inparty, therm_inparty >100, NA) #turn impossible values into NAs
    )

#check
summary(final_drops$therm_inparty)

#Outparty DROPS
final_drops <- final_drops %>% 
  mutate(therm_outparty =
           case_when(
            partyID == 1 ~ as.numeric(Q1_w2), #Rep Warmth towards Dem
            partyID == 0 ~ as.numeric(Q2_w2), #Dem Warmth towards Rep
           ),
         therm_outparty = replace(therm_outparty, therm_outparty >100, NA) #turn impossible values into NAs
    )

#check
summary(final_drops$therm_outparty)

#Feeling Thermometer Difference DROPS 
final_drops <- final_drops %>% 
  mutate(
    therm_diff = therm_inparty - therm_outparty
  )

#check
summary(final_drops$therm_diff)


```


```{r FT W3}

#Wave 3 Inparty DROPS
final_drops <- final_drops %>% 
  mutate(therm_inparty_w3 =
           case_when(
            partyID == 0 ~ as.numeric(Q1_w3), #Dem Warmth toward Dem
            partyID == 1 ~ as.numeric(Q2_w3), #Rep Warmth towards Rep
           ),
         therm_inparty_w3 = replace(therm_inparty_w3, therm_inparty_w3 >100, NA) #turn impossible values into NAs
    )

#Outparty DROPS
final_drops <- final_drops %>% 
  mutate(therm_outparty_w3 =
           case_when(
            partyID == 1 ~ as.numeric(Q1_w3), #Rep Warmth towards Dem
            partyID == 0 ~ as.numeric(Q2_w3), #Dem Warmth towards Rep
           ),
         therm_outparty_w3 = replace(therm_outparty_w3, therm_outparty_w3 >100, NA) #turn impossible values into NAs
    )

#Feeling Thermometer Difference 
final_drops <- final_drops %>% 
  mutate(
    therm_diff_w3 = therm_inparty_w3 - therm_outparty_w3
  )

```

```{r Threat perception}

#Outparty threat perception DROPS
final_drops <- final_drops %>% 
  mutate(threat_outparty =
           case_when(
            Q6_w2 == "OUTPARTY are NOT a serious threat to the United States and its people" ~ 0, 
            Q6_w2 == "OUTPARTY are a serious threat to the United States and its people" ~ 1,
            Q6_w2 == "2" ~ 0,
            Q6_w2 == "1" ~ 1
           )
    )

#check
table(is.na(final_drops$threat_outparty))

```

```{r Threat W3}

#Threat wave 3 DROPS
final_drops <- final_drops %>% 
  mutate(threat_outparty_w3 =
           case_when(
            Q6_w3 == "OUTPARTY are NOT a serious threat to the United States and its people" ~ 0, 
            Q6_w3 == "OUTPARTY are a serious threat to the United States and its people" ~ 1,
            Q6_w3 == "2" ~ 0,
            Q6_w3 == "1" ~ 1
           )
    )

#check
table(is.na(final_drops$threat_outparty_w3))


```

```{r Discomfort}

#####Friends#####

#Discomfort with outparty friends DROPS
final_drops <- final_drops %>% 
  mutate(discomfort_outparty_friends =
           case_when(
            Q3_w2 == "Very comfortable" ~ 0, 
            Q3_w2 == "Somewhat comfortable" ~ 1,
            Q3_w2 == "Not comfortable" ~ 2,
            Q3_w2 == "1" ~ 0, 
            Q3_w2 == "2" ~ 1,
            Q3_w2 == "3" ~ 2
           )
    )

table(is.na(final_drops$discomfort_outparty_friends))

#####Neighbors#####

#Discomfort with outparty neighbors DROPS
final_drops <- final_drops %>% 
  mutate(discomfort_outparty_neighbors =
           case_when(
            Q4_w2 == "Very comfortable" ~ 0, 
            Q4_w2 == "Somewhat comfortable" ~ 1,
            Q4_w2 == "Not comfortable" ~ 2,
            Q4_w2 == "1" ~ 0, 
            Q4_w2 == "2" ~ 1,
            Q4_w2 == "3" ~ 2
           )
    )

table(is.na(final_drops$discomfort_outparty_friends))

#####Marriage#####

#Discomfort with outparty marriage DROPS
final_drops <- final_drops %>% 
  mutate(discomfort_outparty_marriage =
           case_when(
            Q5_w2 == "Very comfortable" ~ 0, 
            Q5_w2 == "Somewhat comfortable" ~ 1,
            Q5_w2 == "Not comfortable" ~ 2,
            Q5_w2 == "1" ~ 0, 
            Q5_w2 == "2" ~ 1,
            Q5_w2 == "3" ~ 2
           )
    ) 

table(is.na(final_drops$discomfort_outparty_marriage))


```

```{r Discomfort W3}


#Discomfort with outparty friends wave 3 DROPS
final_drops <- final_drops %>% 
  mutate(discomfort_outparty_friends_w3 =
           case_when(
            Q3_w3 == "Very comfortable" ~ 0, 
            Q3_w3 == "Somewhat comfortable" ~ 1,
            Q3_w3 == "Not comfortable" ~ 2,
            Q3_w3 == "1" ~ 0, 
            Q3_w3 == "2" ~ 1,
            Q3_w3 == "3" ~ 2
           )
    )

table(is.na(final_drops$discomfort_outparty_friends_w3))


#Discomfort with outparty neighbors DROPS
final_drops <- final_drops %>% 
  mutate(discomfort_outparty_neighbors_w3 =
           case_when(
            Q4_w3 == "Very comfortable" ~ 0, 
            Q4_w3 == "Somewhat comfortable" ~ 1,
            Q4_w3 == "Not comfortable" ~ 2,
            Q4_w3 == "1" ~ 0, 
            Q4_w3 == "2" ~ 1,
            Q4_w3 == "3" ~ 2
           )
    )

#check
table(is.na(final_drops$discomfort_outparty_neighbors_w3))


#Discomfort with outparty marriage DROPS
final_drops <- final_drops %>% 
  mutate(discomfort_outparty_marriage_w3 =
           case_when(
            Q5_w3 == "Very comfortable" ~ 0, 
            Q5_w3 == "Somewhat comfortable" ~ 1,
            Q5_w3 == "Not comfortable" ~ 2,
            Q5_w3 == "1" ~ 0, 
            Q5_w3 == "2" ~ 1,
            Q5_w3 == "3" ~ 2
           )
    ) 

#check
table(is.na(final_drops$discomfort_outparty_marriage_w3))


```

```{r Negative partisanship}

table(final_drops$Q7_w2)

#Negative partisanship DROPS
final_drops <- final_drops %>% 
  mutate(neg_partisan =
           case_when(
            Q7_w2 == "I am a $IN_party because of what the $IN_party$s represent" ~ 0, 
            Q7_lean_w2 == "I lean or am a $IN_party because of what the $IN_party$s represent" ~ 0,
            Q7_w2 == "1" ~ 0,
            Q7_lean_w2 == "1" ~ 0,
            Q7_w2 == "I am a $IN_party because I am against what the $OUT_party$s represent" ~ 1,
            Q7_lean_w2 == "I lean or am a $IN_party because I am against what the $OUT_party$s represent" ~ 1,
            Q7_w2 == "2" ~ 1,
            Q7_lean_w2 == "2" ~ 1,
           )
    ) 

#check
table(is.na(final_drops$neg_partisan))

```


```{r Negative partisanship W3}

#Negative partisanship Wave 3 DROPS
final_drops <- final_drops %>% 
  mutate(neg_partisan_w3 =
           case_when(
            Q7_w3 == "I am a $IN_party because of what the $IN_party$s represent" ~ 0, 
            Q7_lean_w3 == "I lean or am a $IN_party because of what the $IN_party$s represent" ~ 0,
            Q7_w3 == "1" ~ 0,
            Q7_lean_w3 == "1" ~ 0,
            Q7_w3 == "I am a $IN_party because I am against what the $OUT_party$s represent" ~ 1,
            Q7_lean_w3 == "I lean or am a $IN_party because I am against what the $OUT_party$s represent" ~ 1,
            Q7_w3 == "2" ~ 1,
            Q7_lean_w3 == "2" ~ 1
           )
    ) 

#check
table(is.na(final_drops$neg_partisan_w3))

```

```{r Trust}

#####INPARTY#####

#In-party trust DROPS
final_drops <- final_drops %>% 
  mutate(trust_inparty =
           case_when(
            Q25_w2 == "Almost never" ~ 0,
            Q25_w2 == "1" ~ 0,
            Q25_w2 == "Once in a while" ~ 1,
            Q25_w2 == "2" ~ 1,
            Q25_w2 == "About half the time" ~ 2,
            Q25_w2 == "3" ~ 2,
            Q25_w2 == "Most of the time" ~ 3,
            Q25_w2 == "4" ~3,
            Q25_w2 == "Almost always" ~ 4,
            Q25_w2 == "5" ~ 4
           )
    ) 

table(is.na(final_drops$trust_inparty))

#####OUTPARTY#####

#Outparty trust DROPS
final_drops <- final_drops %>% 
  mutate(trust_outparty =
           case_when(
            Q38_w2 == "Almost never" ~ 0,
            Q38_w2 == "1" ~ 0,
            Q38_w2 == "Once in a while" ~ 1,
            Q38_w2 == "2" ~ 1,
            Q38_w2 == "About half the time" ~ 2,
            Q38_w2 == "3" ~ 2,
            Q38_w2 == "Most of the time" ~ 3,
            Q38_w2 == "4" ~3,
            Q38_w2 == "Almost always" ~ 4,
            Q38_w2 == "5" ~ 4
           )
    ) 

#####DIFFERENCE#####


#Difference in trust DROPS
final_drops <- final_drops %>% 
  mutate(
    trust_diff = trust_inparty - trust_outparty
  )

summary(final_drops$trust_inparty)
summary(final_drops$trust_outparty)


```


```{r Trust W3}

#In-party trust DROPS
final_drops <- final_drops %>% 
  mutate(trust_inparty_w3 =
           case_when(
            Q25_w3 == "Almost never" ~ 0,
            Q25_w3 == "1" ~ 0,
            Q25_w3 == "Once in a while" ~ 1,
            Q25_w3 == "2" ~ 1,
            Q25_w3 == "About half the time" ~ 2,
            Q25_w3 == "3" ~ 2,
            Q25_w3 == "Most of the time" ~ 3,
            Q25_w3 == "4" ~3,
            Q25_w3 == "Almost always" ~ 4,
            Q25_w3 == "5" ~ 4
           )
    )

#check
table(is.na(final_drops$trust_inparty_w3))


#Outparty trust DROPS
final_drops <- final_drops %>% 
  mutate(trust_outparty_w3 =
           case_when(
            Q38_w3 == "Almost never" ~ 0,
            Q38_w3 == "1" ~ 0,
            Q38_w3 == "Once in a while" ~ 1,
            Q38_w3 == "2" ~ 1,
            Q38_w3 == "About half the time" ~ 2,
            Q38_w3 == "3" ~ 2,
            Q38_w3 == "Most of the time" ~ 3,
            Q38_w3 == "4" ~3,
            Q38_w3 == "Almost always" ~ 4,
            Q38_w3 == "5" ~ 4
           )
    ) 


#Difference in trust DROPS
final_drops <- final_drops %>% 
  mutate(
    trust_diff_w3 = trust_inparty_w3 - trust_outparty_w3
  )

#check
table(is.na(final_drops$trust_diff_w3))

```


### Index Construction


```{r Affective Polarization Index}

#construct index: scale relevant variables first 
final_drops <- final_drops %>%
  mutate(therm_diff_scale = scale(therm_diff),
         threat_outparty_scale = scale(threat_outparty),
         discomfort_outparty_friends_scale = scale(discomfort_outparty_friends),
         discomfort_outparty_neighbors_scale = scale(discomfort_outparty_neighbors),
         discomfort_outparty_marriage_scale = scale(discomfort_outparty_marriage),
         neg_partisan_scale = scale(neg_partisan), 
         trust_diff_scale = scale(trust_diff)
  ) 

#create new keys list
keys <- list(final_drops=cs(therm_diff_scale, threat_outparty_scale, discomfort_outparty_friends_scale,discomfort_outparty_neighbors_scale, discomfort_outparty_marriage_scale,neg_partisan_scale, trust_diff_scale))

#use alpha function to calcualte scores
tmp <- alpha(final_drops, keys, check.keys = T, use ="pairwise")

#create new variable 
final_drops <- final_drops %>% 
  mutate(
    aff_pol_idx = tmp$scores
  )

#check
summary(final_drops$aff_pol_idx)  
table(is.na(final_drops$aff_pol_idx))

#remove tmp/keys
remove(tmp)
remove(keys)

```


```{r Affective Polarization Index Outparty}

#outparty scaled variables
final_drops <- final_drops %>%
  mutate(therm_outparty_scale = scale(therm_outparty)*-1, #reverse sign
         trust_outparty_scale = scale(trust_outparty)*-1  #reverse sign
  ) 

#create keys list
keys_outparty <- list(final_drops=cs(therm_outparty_scale, threat_outparty_scale, discomfort_outparty_friends_scale,discomfort_outparty_neighbors_scale, discomfort_outparty_marriage_scale,neg_partisan_scale, trust_outparty_scale))

#use alpha function to calculate scores
tmp_out <- alpha(final_drops, keys_outparty, check.keys = T, use ="pairwise")

#create new variable 
final_drops <- final_drops %>% 
  mutate(
    aff_pol_idx_outparty = tmp_out$scores
  )

#check (looks good)
summary(final_drops$aff_pol_idx_outparty) 

#remove tmp/keys
remove(tmp_out)
remove(keys_outparty)


```


```{r Affective Polarization Index No Neg Partisanship}


#create relevant keys
keys_noneg <- list(final_drops=cs(therm_diff_scale, threat_outparty_scale, discomfort_outparty_friends_scale,discomfort_outparty_neighbors_scale, discomfort_outparty_marriage_scale, trust_diff_scale))

#use alpha function to calculate scores
tmp_noneg <- alpha(final_drops, keys_noneg, check.keys = T, use ="pairwise")

#create new variable 
final_drops <- final_drops %>% 
  mutate(
    aff_pol_idx_noneg = tmp_noneg$scores
  )

#check
summary(final_drops$aff_pol_idx_noneg)


#remove tmp/keys
remove(tmp_noneg)
remove(keys_noneg)

```


```{r Affective Pol Index W3}

#Drops
#create new scaled variables
final_drops <- final_drops %>%
  mutate(therm_diff_w3_scale = scale(therm_diff_w3), 
         threat_outparty_w3_scale = scale(threat_outparty_w3),
         discomfort_outparty_friends_w3_scale = scale(discomfort_outparty_friends_w3),
         discomfort_outparty_neighbors_w3_scale = scale(discomfort_outparty_neighbors_w3), 
         discomfort_outparty_marriage_w3_scale = scale(discomfort_outparty_marriage_w3), 
         neg_partisan_w3_scale = scale(neg_partisan_w3)*-1, #reverse sign
         trust_diff_w3_scale = scale(trust_diff_w3),
         therm_outparty_w3_scale = scale(therm_outparty_w3)*-1, #reverse sign 
         trust_outparty_w3_scale = scale(trust_outparty_w3)*-1 #reverse sign 
  ) 

#create keys
keys_w3 <- list(final_drops=cs(therm_diff_w3_scale, threat_outparty_w3_scale, discomfort_outparty_friends_w3_scale,discomfort_outparty_neighbors_w3_scale, discomfort_outparty_marriage_w3_scale,neg_partisan_w3_scale, trust_diff_w3_scale))

#use alpha function to calcualte scores
tmp_w3 <- alpha(final_drops, keys_w3, check.keys = T, use ="pairwise") 

#create new variable 
final_drops <- final_drops %>% 
  mutate(
    aff_pol_idx_w3 = tmp_w3$scores
  )


#check
summary(final_drops$aff_pol_idx_w3)

#####OUTPARTY#####

#unreverse neg partisan
final_drops <- final_drops %>% 
  mutate(
    neg_partisan_w3_scale = scale(neg_partisan_w3)
  )

#outparty keys
keys_w3_out <- list(final_drops=cs(therm_outparty_w3_scale, threat_outparty_w3_scale, discomfort_outparty_friends_w3_scale,discomfort_outparty_neighbors_w3_scale, discomfort_outparty_marriage_w3_scale,neg_partisan_w3_scale, trust_outparty_w3_scale))

#use alpha function to calcualte scores
tmp_w3_out <- alpha(final_drops, keys_w3_out, check.keys = T, use ="pairwise")

tmp_w3_out$keys

#create new variable 
final_drops <- final_drops %>% 
  mutate(
    aff_pol_idx_outparty_w3 = tmp_w3_out$scores
  )

#check
summary(final_drops$aff_pol_idx_w3)

#####NO_NEG#####

#DROPS

#no neg partisanship keys
keys_w3_noneg <- list(final_drops=cs(therm_diff_w3_scale, threat_outparty_w3_scale, discomfort_outparty_friends_w3_scale,discomfort_outparty_neighbors_w3_scale, discomfort_outparty_marriage_w3_scale, trust_diff_w3_scale))

#use alpha function to calcualte scores
tmp_w3_noneg <- alpha(final_drops, keys_w3_noneg, check.keys = T, use ="pairwise")

#create new variable 
final_drops <- final_drops %>% 
  mutate(
    aff_pol_idx_noneg_w3 = tmp_w3_noneg$scores
  )

#check
summary(final_drops$aff_pol_idx_noneg_w3)

#remove vestiges
remove(keys_w3)
remove(keys_w3_noneg)
remove(keys_w3_out)
remove(tmp_w3)
remove(tmp_w3_out)
remove(tmp_w3_noneg)

```


## Behavioral measures cleaning

```{r BA newsletter}

#interest
final_drops <- final_drops %>% 
  mutate(BA_newsletter =
           case_when(
            Q76_w2 == "No" ~ 0,
            Q76_w2 == "Yes" ~ 1,
            Q76_w2 == "2" ~ 0,
            Q76_w2 == "1" ~ 1
           )
    )

#check
table(is.na(final_drops$BA_newsletter))


#actual clicks 
final_drops <- final_drops %>% 
  mutate(BA_newsletter_clicked =
           case_when(
            #Q76_w2 == "8" ~ NA_real_,
            #Q76_w2 == "not asked" ~ NA_real_,
            #Q76_w2 == "skipped" ~ NA_real_,
            Q87_links_clicked_w2 == "https://braverangels.org/subscribe/" ~ 1,
            Q87_links_clicked_w2 == "https://braverangels.org/subscribe/-https://braverangels.org/subscribe/" ~ 1,
            Q87_links_clicked_w2 == "https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/" ~ 1,
            Q87_links_clicked_w2 == "https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/" ~1,
            Q87_links_clicked_w2 == "https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/-https://braverangels.org/subscribe/" ~ 1,
            Q87_links_clicked_w2 == "__NA__" ~ 0
           )
    )


#check
table(final_drops$BA_newsletter_clicked)


```


```{r donations}

#donations
final_drops <- final_drops %>% 
  mutate(donate_allsides = case_when(
    Q78_w2 == "999" ~ NA_real_,
    Q78_w2 == "not asked" ~NA_real_,
    Q78_w2 == "No Data" ~NA_real_,
    TRUE ~  as.numeric(Q78_w2)
  ),
         donate_lrc = case_when(
    Q79_w2 == "999" ~ NA_real_,
    Q79_w2 == "not asked" ~NA_real_,
    Q79_w2 == "No Data" ~NA_real_,
    TRUE ~  as.numeric(Q79_w2)
  ),
         donate_BA = case_when(
    Q80_w2 == "999" ~ NA_real_,
    Q80_w2 == "998" ~ NA_real_,
    Q80_w2 == "not asked" ~NA_real_,
    Q80_w2 == "No Data" ~NA_real_,
    TRUE ~  as.numeric(Q80_w2)
  ))
         

#Q77
98+317 #415 say they want to donate 
174+1503 #1681 say they don't want
382 + 29 +2 +2 #415 were not asked or we have No Data or skipped
# 66 pure NAs

176+382+1534
2573-2092

#clean up allsides
final_drops <- final_drops %>% 
  mutate(donate_allsides = 
           case_when(
             Q77_w2 == "No" ~ 0,
             Q77_w2 == "2" ~ 0,
             donate_allsides >= 0 ~ as.numeric(donate_allsides)
           )
         )


#check (481 NAs)
summary(final_drops$donate_allsides)

#clean up LRC
final_drops <- final_drops %>% 
  mutate(donate_lrc = 
           case_when(
             Q77_w2 == "No" ~ 0,
             Q77_w2 == "2" ~ 0,
             donate_lrc >= 0 ~ as.numeric(donate_lrc)
           )
         )

#check (482 NAs)
summary(final_drops$donate_lrc)
table(is.na(final_drops$donate_lrc))

#clean up BA
final_drops <- final_drops %>% 
  mutate(donate_BA = 
           case_when(
             Q77_w2 == "No" ~ 0,
             Q77_w2 == "2" ~ 0,
             donate_BA >= 0 ~ as.numeric(donate_BA)
           )
         )

#check (481 NAs)
summary(final_drops$donate_BA)
table(is.na(final_drops$donate_BA))


#create all and any variables
final_drops <- final_drops %>% 
  mutate(
    donate_all = donate_allsides + donate_lrc + donate_BA,
    donate_any = donate_all > 0
  )

summary(final_drops$donate_all)

```


```{r Unify America Sign-up}

#Clicked Sign-up for Unify America 
final_drops <- final_drops %>% 
  mutate(
    UA_interest = case_when(
      Q82_w3_signedup == "1.0" ~ 1, 
      Q82_w3_signedup == "1" ~1,
      Q82_w3_signedup == "skipped" ~ 0,
      Q82_w3_signedup == "8" ~ 0
      )
  )

#check
table(is.na(final_drops$UA_interest))


```


## Anti democratic attitudes index

```{r anti-democratic attitudes}

table(final_drops$Q60_w2)

#name variables 
final_drops <- final_drops %>% 
  mutate(
    antidem1_rep = Q60_w2, #reps
    antidem2_rep = Q61_w2,
    antidem3_rep = Q62_w2,
    antidem4_rep = Q63_w2,
    antidem5_rep = Q64_w2,
    antidem6_rep = Q65_w2, 
    antidem1_dem = Q66_w2, #dems 
    antidem2_dem = Q67_w2,
    antidem3_dem = Q68_w2,
    antidem4_dem = Q69_w2,
    antidem5_dem = Q70_w2,
    antidem6_dem = Q71_w2
  )

#anti_dem1 
final_drops <- final_drops %>% 
  mutate(antidem1 =
    case_when(
      antidem1_rep == "Strongly oppose" ~ 0, #reps
      antidem1_rep == "4" ~ 0,
      antidem1_rep == "Somewhat oppose" ~ 1,
      antidem1_rep == "3" ~ 1,
      antidem1_rep == "Somewhat support" ~ 2,
      antidem1_rep == "2" ~ 2,
      antidem1_rep == "Strongly support" ~ 3,
      antidem1_rep == "1" ~ 3,
      antidem1_dem == "Strongly oppose" ~ 0, #dems 
      antidem1_dem == "4" ~ 0,
      antidem1_dem == "Somewhat oppose" ~ 1,
      antidem1_dem == "3" ~ 1,
      antidem1_dem == "Somewhat support" ~ 2,
      antidem1_dem == "2" ~ 2,
      antidem1_dem == "Strongly support" ~ 3,
      antidem1_dem == "1" ~ 3
    )
  )

#check
table(is.na(final_drops$antidem1))
summary(final_drops$antidem1)


#anti_dem2 
final_drops <- final_drops %>% 
  mutate(antidem2 =
    case_when(
      antidem2_rep == "Strongly oppose" ~ 0, #reps
      antidem2_rep == "4" ~ 0,
      antidem2_rep == "Somewhat oppose" ~ 1,
      antidem2_rep == "3" ~ 1,
      antidem2_rep == "Somewhat support" ~ 2,
      antidem2_rep == "2" ~ 2,
      antidem2_rep == "Strongly support" ~ 3,
      antidem2_rep == "1" ~ 3,
      antidem2_dem == "Strongly oppose" ~ 0, #dems 
      antidem2_dem == "4" ~ 0,
      antidem2_dem == "Somewhat oppose" ~ 1,
      antidem2_dem == "3" ~ 1,
      antidem2_dem == "Somewhat support" ~ 2,
      antidem2_dem == "2" ~ 2,
      antidem2_dem == "Strongly support" ~ 3,
      antidem2_dem == "1" ~ 3
    )
  )

#check
table(is.na(final_drops$antidem2))


#anti_dem3
final_drops <- final_drops %>% 
  mutate(antidem3 =
    case_when(
      antidem3_rep == "Strongly oppose" ~ 0, #reps 
      antidem3_rep == "4" ~ 0,
      antidem3_rep == "Somewhat oppose" ~ 1,
      antidem3_rep == "3" ~ 1,
      antidem3_rep == "Somewhat support" ~ 2,
      antidem3_rep == "2" ~ 2,
      antidem3_rep == "Strongly support" ~ 3,
      antidem3_rep == "1" ~ 3,
      antidem3_dem == "Strongly oppose" ~ 0, #dems 
      antidem3_dem == "4" ~ 0,
      antidem3_dem == "Somewhat oppose" ~ 1,
      antidem3_dem == "3" ~ 1,
      antidem3_dem == "Somewhat support" ~ 2,
      antidem3_dem == "2" ~ 2,
      antidem3_dem == "Strongly support" ~ 3,
      antidem3_dem == "1" ~ 3
    )
  )

#anti_dem4 
final_drops <- final_drops %>% 
  mutate(antidem4 =
    case_when(
      antidem4_rep == "Strongly oppose" ~ 0, #reps 
      antidem4_rep == "4" ~ 0,
      antidem4_rep == "Somewhat oppose" ~ 1,
      antidem4_rep == "3" ~ 1,
      antidem4_rep == "Somewhat support" ~ 2,
      antidem4_rep == "2" ~ 2,
      antidem4_rep == "Strongly support" ~ 3,
      antidem4_rep == "1" ~ 3,
      antidem4_dem == "Strongly oppose" ~ 0, #dems 
      antidem4_dem == "4" ~ 0,
      antidem4_dem == "Somewhat oppose" ~ 1,
      antidem4_dem == "3" ~ 1,
      antidem4_dem == "Somewhat support" ~ 2,
      antidem4_dem == "2" ~ 2,
      antidem4_dem == "Strongly support" ~ 3,
      antidem4_dem == "1" ~ 3
    )
  )

#anti_dem5 
final_drops <- final_drops %>% 
  mutate(antidem5 =
    case_when(
      antidem5_rep == "Strongly oppose" ~ 0, #reps
      antidem5_rep == "4" ~ 0,
      antidem5_rep == "Somewhat oppose" ~ 1,
      antidem5_rep == "3" ~ 1,
      antidem5_rep == "Somewhat support" ~ 2,
      antidem5_rep == "2" ~ 2,
      antidem5_rep == "Strongly support" ~ 3,
      antidem5_rep == "1" ~ 3,
      antidem5_dem == "Strongly oppose" ~ 0, # dems
      antidem5_dem == "4" ~ 0,
      antidem5_dem == "Somewhat oppose" ~ 1,
      antidem5_dem == "3" ~ 1,
      antidem5_dem == "Somewhat support" ~ 2,
      antidem5_dem == "2" ~ 2,
      antidem5_dem == "Strongly support" ~ 3,
      antidem5_dem == "1" ~ 3
    )
  )

#anti_dem6 
final_drops <- final_drops %>% 
  mutate(antidem6 =
    case_when(
      antidem6_rep == "Strongly oppose" ~ 0, #reps 
      antidem6_rep == "4" ~ 0,
      antidem6_rep == "Somewhat oppose" ~ 1,
      antidem6_rep == "3" ~ 1,
      antidem6_rep == "Somewhat support" ~ 2,
      antidem6_rep == "2" ~ 2,
      antidem6_rep == "Strongly support" ~ 3,
      antidem6_rep == "1" ~ 3,
      antidem6_dem == "Strongly oppose" ~ 0, #dems 
      antidem6_dem == "4" ~ 0,
      antidem6_dem == "Somewhat oppose" ~ 1,
      antidem6_dem == "3" ~ 1,
      antidem6_dem == "Somewhat support" ~ 2,
      antidem6_dem == "2" ~ 2,
      antidem6_dem == "Strongly support" ~ 3,
      antidem6_dem == "1" ~ 3
    )
  )

#check
table(is.na(final_drops$antidem6))

```

```{r anti-dem W3}

#name variables wave 3
final_drops <- final_drops %>% 
  mutate(
    antidem1_rep_w3 = Q60_w3,
    antidem2_rep_w3 = Q61_w3,
    antidem3_rep_w3 = Q62_w3,
    antidem4_rep_w3 = Q63_w3,
    antidem5_rep_w3 = Q64_w3,
    antidem6_rep_w3 = Q65_w3, 
    antidem1_dem_w3 = Q66_w3,
    antidem2_dem_w3 = Q67_w3,
    antidem3_dem_w3 = Q68_w3,
    antidem4_dem_w3 = Q69_w3,
    antidem5_dem_w3 = Q70_w3,
    antidem6_dem_w3 = Q71_w3
  )

#anti_dem1 
final_drops <- final_drops %>% 
  mutate(antidem1_w3 =
    case_when(
      antidem1_rep_w3 == "Strongly oppose" ~ 0,
      antidem1_rep_w3 == "4" ~ 0,
      antidem1_rep_w3 == "Somewhat oppose" ~ 1,
      antidem1_rep_w3 == "3" ~ 1,
      antidem1_rep_w3 == "Somewhat support" ~ 2,
      antidem1_rep_w3 == "2" ~ 2,
      antidem1_rep_w3 == "Strongly support" ~ 3,
      antidem1_rep_w3 == "1" ~ 3,
      antidem1_dem_w3 == "Strongly oppose" ~ 0,
      antidem1_dem_w3 == "4" ~ 0,
      antidem1_dem_w3 == "Somewhat oppose" ~ 1,
      antidem1_dem_w3 == "3" ~ 1,
      antidem1_dem_w3 == "Somewhat support" ~ 2,
      antidem1_dem_w3 == "2" ~ 2,
      antidem1_dem_w3 == "Strongly support" ~ 3,
      antidem1_dem_w3 == "1" ~ 3
    )
  )

#check
table(final_drops$antidem1_w3)

#anti_dem2 
final_drops <- final_drops %>% 
  mutate(antidem2_w3 =
    case_when(
      antidem2_rep_w3 == "Strongly oppose" ~ 0,
      antidem2_rep_w3 == "4" ~ 0,
      antidem2_rep_w3 == "Somewhat oppose" ~ 1,
      antidem2_rep_w3 == "3" ~ 1,
      antidem2_rep_w3 == "Somewhat support" ~ 2,
      antidem2_rep_w3 == "2" ~ 2,
      antidem2_rep_w3 == "Strongly support" ~ 3,
      antidem2_rep_w3 == "1" ~ 3,
      antidem2_dem_w3 == "Strongly oppose" ~ 0,
      antidem2_dem_w3 == "4" ~ 0,
      antidem2_dem_w3 == "Somewhat oppose" ~ 1,
      antidem2_dem_w3 == "3" ~ 1,
      antidem2_dem_w3 == "Somewhat support" ~ 2,
      antidem2_dem_w3 == "2" ~ 2,
      antidem2_dem_w3 == "Strongly support" ~ 3,
      antidem2_dem_w3 == "1" ~ 3
    )
  )


#anti_dem3
final_drops <- final_drops %>% 
  mutate(antidem3_w3 =
    case_when(
      antidem3_rep_w3 == "Strongly oppose" ~ 0,
      antidem3_rep_w3 == "4" ~ 0,
      antidem3_rep_w3 == "Somewhat oppose" ~ 1,
      antidem3_rep_w3 == "3" ~ 1,
      antidem3_rep_w3 == "Somewhat support" ~ 2,
      antidem3_rep_w3 == "2" ~ 2,
      antidem3_rep_w3 == "Strongly support" ~ 3,
      antidem3_rep_w3 == "1" ~ 3,
      antidem3_dem_w3 == "Strongly oppose" ~ 0,
      antidem3_dem_w3 == "4" ~ 0,
      antidem3_dem_w3 == "Somewhat oppose" ~ 1,
      antidem3_dem_w3 == "3" ~ 1,
      antidem3_dem_w3 == "Somewhat support" ~ 2,
      antidem3_dem_w3 == "2" ~ 2,
      antidem3_dem_w3 == "Strongly support" ~ 3,
      antidem3_dem_w3 == "1" ~ 3
    )
  )

#anti_dem4 
final_drops <- final_drops %>% 
  mutate(antidem4_w3 =
    case_when(
      antidem4_rep_w3 == "Strongly oppose" ~ 0,
      antidem4_rep_w3 == "4" ~ 0,
      antidem4_rep_w3 == "Somewhat oppose" ~ 1,
      antidem4_rep_w3 == "3" ~ 1,
      antidem4_rep_w3 == "Somewhat support" ~ 2,
      antidem4_rep_w3 == "2" ~ 2,
      antidem4_rep_w3 == "Strongly support" ~ 3,
      antidem4_rep_w3 == "1" ~ 3,
      antidem4_dem_w3 == "Strongly oppose" ~ 0,
      antidem4_dem_w3 == "4" ~ 0,
      antidem4_dem_w3 == "Somewhat oppose" ~ 1,
      antidem4_dem_w3 == "3" ~ 1,
      antidem4_dem_w3 == "Somewhat support" ~ 2,
      antidem4_dem_w3 == "2" ~ 2,
      antidem4_dem_w3 == "Strongly support" ~ 3,
      antidem4_dem_w3 == "1" ~ 3
    )
  )

#anti_dem5 
final_drops <- final_drops %>% 
  mutate(antidem5_w3 =
    case_when(
      antidem5_rep_w3 == "Strongly oppose" ~ 0,
      antidem5_rep_w3 == "4" ~ 0,
      antidem5_rep_w3 == "Somewhat oppose" ~ 1,
      antidem5_rep_w3 == "3" ~ 1,
      antidem5_rep_w3 == "Somewhat support" ~ 2,
      antidem5_rep_w3 == "2" ~ 2,
      antidem5_rep_w3 == "Strongly support" ~ 3,
      antidem5_rep_w3 == "1" ~ 3,
      antidem5_dem_w3 == "Strongly oppose" ~ 0,
      antidem5_dem_w3 == "4" ~ 0,
      antidem5_dem_w3 == "Somewhat oppose" ~ 1,
      antidem5_dem_w3 == "3" ~ 1,
      antidem5_dem_w3 == "Somewhat support" ~ 2,
      antidem5_dem_w3 == "2" ~ 2,
      antidem5_dem_w3 == "Strongly support" ~ 3,
      antidem5_dem_w3 == "1" ~ 3
    )
  )

#anti_dem6 
final_drops <- final_drops %>% 
  mutate(antidem6_w3 =
    case_when(
      antidem6_rep_w3 == "Strongly oppose" ~ 0,
      antidem6_rep_w3 == "4" ~ 0,
      antidem6_rep_w3 == "Somewhat oppose" ~ 1,
      antidem6_rep_w3 == "3" ~ 1,
      antidem6_rep_w3 == "Somewhat support" ~ 2,
      antidem6_rep_w3 == "2" ~ 2,
      antidem6_rep_w3 == "Strongly support" ~ 3,
      antidem6_rep_w3 == "1" ~ 3,
      antidem6_dem_w3 == "Strongly oppose" ~ 0,
      antidem6_dem_w3 == "4" ~ 0,
      antidem6_dem_w3 == "Somewhat oppose" ~ 1,
      antidem6_dem_w3 == "3" ~ 1,
      antidem6_dem_w3 == "Somewhat support" ~ 2,
      antidem6_dem_w3 == "2" ~ 2,
      antidem6_dem_w3 == "Strongly support" ~ 3,
      antidem6_dem_w3 == "1" ~ 3
    )
  )

#check
table(is.na(final_drops$antidem6_w3))


```

```{r anti-dem index}

#create scaled variables 
final_drops <- final_drops %>% 
  mutate(
      antidem1_scale = scale(antidem1),
      antidem2_scale = scale(antidem2),
      antidem3_scale = scale(antidem3),
      antidem4_scale = scale(antidem4),
      antidem5_scale = scale(antidem5),
      antidem6_scale = scale(antidem6)
  )

#create keys 
keys_dem <- list(final_drops=cs(antidem1_scale, antidem2_scale, antidem3_scale,
                                             antidem4_scale, antidem5_scale, antidem6_scale)
                                 )

#alpha function 
tmp <- alpha(final_drops, keys_dem, check.keys = T, use ="pairwise")


#create indexed variable
final_drops <- final_drops %>% 
  mutate(
    antidem_idx = tmp$scores
  )

#check
summary(final_drops$antidem_idx)

#remove
remove(tmp)
remove(keys_dem)


#wave 3 anti-dem index

#create scaled variables for wave 3
final_drops <- final_drops %>% 
  mutate(
      antidem1_scale_w3 = scale(antidem1_w3),
      antidem2_scale_w3 = scale(antidem2_w3),
      antidem3_scale_w3 = scale(antidem3_w3),
      antidem4_scale_w3 = scale(antidem4_w3),
      antidem5_scale_w3 = scale(antidem5_w3),
      antidem6_scale_w3 = scale(antidem6_w3)
  )


#create keys 
keys_dem_w3 <- list(final_drops=
                      cs(antidem1_scale_w3, antidem2_scale_w3, antidem3_scale_w3,
                         antidem4_scale_w3, antidem5_scale_w3, antidem6_scale_w3))

#alpha function 
tmp_w3 <- alpha(final_drops, keys_dem_w3, check.keys = T, use ="pairwise")

#create new variable
final_drops <- final_drops %>% 
  mutate(
    antidem_idx_w3 = tmp_w3$scores
  )

#check
summary(final_drops$antidem_idx_w3)

#remove
remove(tmp_w3)
remove(keys_dem_w3)

```


## Stereotypes indices

List of attributes that are part of pre-registered index:
-Patriotic (pos)
-Intelligent (pos)
-Honest (pos)
-Open Minded (pos)
-Generous (pos)
-Hypocritical (neg)
-Selfish (neg)
-Mean (neg)

Additions:
-Decent (pos)
-Unethical (neg)
-Uncaring (neg)

Generous is asked twice in wave 2

```{r stereotyping renaming}

table(final_drops$Q13_w2_w2)

#Patriotic inparty
final_drops <- final_drops %>% 
  mutate(patriotic_inparty =
    case_when(
      Q13_w2_w2 == "Does not apply" ~ 0,
      Q13_w2_w2 == "Applies somewhat" ~ 1,
      Q13_w2_w2 == "Applies well" ~ 2,
      Q13_w2_w2 == "1" ~ 0,
      Q13_w2_w2 == "2" ~ 1,
      Q13_w2_w2 == "3" ~ 2
    )
  )

#check
table(is.na(final_drops$patriotic_inparty))

#Patriotic outparty 
final_drops <- final_drops %>% 
  mutate(patriotic_outparty =
    case_when(
      Q26_w2_w2 == "Does not apply" ~ 0,
      Q26_w2_w2 == "Applies somewhat" ~ 1,
      Q26_w2_w2 == "Applies well" ~ 2,
      Q26_w2_w2 == "1" ~ 0,
      Q26_w2_w2 == "2" ~ 1,
      Q26_w2_w2 == "3" ~ 2
    )
  )

#check
table(is.na(final_drops$patriotic_outparty))
  

#Intelligent inparty 
final_drops <- final_drops %>% 
  mutate(intelligent_inparty =
    case_when(
      Q14_w2_w2 == "Does not apply" ~ 0,
      Q14_w2_w2 == "Applies somewhat" ~ 1,
      Q14_w2_w2 == "Applies well" ~ 2,
      Q14_w2_w2 == "1" ~ 0,
      Q14_w2_w2 == "2" ~ 1,
      Q14_w2_w2 == "3" ~ 2
    )
  )

#check
table(is.na(final_drops$intelligent_inparty))

#Intelligent outparty
final_drops <- final_drops %>% 
  mutate(intelligent_outparty =
    case_when(
      Q27_w2_w2 == "Does not apply" ~ 0,
      Q27_w2_w2 == "Applies somewhat" ~ 1,
      Q27_w2_w2 == "Applies well" ~ 2,
      Q27_w2_w2 == "1" ~ 0,
      Q27_w2_w2 == "2" ~ 1,
      Q27_w2_w2 == "3" ~ 2
    )
  )

#Honest inparty 
final_drops <- final_drops %>% 
  mutate(honest_inparty =
    case_when(
      Q15_w2_w2 == "Does not apply" ~ 0,
      Q15_w2_w2 == "Applies somewhat" ~ 1,
      Q15_w2_w2 == "Applies well" ~ 2,
      Q15_w2_w2 == "1" ~ 0,
      Q15_w2_w2 == "2" ~ 1,
      Q15_w2_w2 == "3" ~ 2
    )
  )

#Honest outparty
final_drops <- final_drops %>% 
  mutate(honest_outparty =
    case_when(
      Q28_w2_w2 == "Does not apply" ~ 0,
      Q28_w2_w2 == "Applies somewhat" ~ 1,
      Q28_w2_w2 == "Applies well" ~ 2,
      Q28_w2_w2 == "1" ~ 0,
      Q28_w2_w2 == "2" ~1,
      Q28_w2_w2 == "3" ~ 2
    )
  )


#Open minded inparty 
final_drops <- final_drops %>% 
  mutate(openminded_inparty =
    case_when(
      Q16_w2_w2 == "Does not apply" ~ 0,
      Q16_w2_w2 == "Applies somewhat" ~ 1,
      Q16_w2_w2 == "Applies well" ~ 2,
      Q16_w2_w2 == "1" ~ 0,
      Q16_w2_w2 == "2" ~ 1,
      Q16_w2_w2 == "3" ~ 2
    )
  )

#Open minded outparty 
final_drops <- final_drops %>% 
  mutate(openminded_outparty =
    case_when(
      Q29_w2_w2 == "Does not apply" ~ 0,
      Q29_w2_w2 == "Applies somewhat" ~ 1,
      Q29_w2_w2 == "Applies well" ~ 2,
      Q29_w2_w2 == "1" ~ 0,
      Q29_w2_w2 == "2" ~ 1,
      Q29_w2_w2 == "3" ~ 2
    )
  )


#Generous inparty
final_drops <- final_drops %>% 
  mutate(generous_inparty =
    case_when(
      Q17_w2_w2 == "Does not apply" ~ 0,
      Q17_w2_w2 == "Applies somewhat" ~ 1,
      Q17_w2_w2 == "Applies well" ~ 2,
      Q17_w2_w2 == "1" ~ 0,
      Q17_w2_w2 == "2" ~ 1,
      Q17_w2_w2 == "3" ~ 2
    )
  )

#Generous outparty
final_drops <- final_drops %>% 
  mutate(generous_outparty =
    case_when(
      Q30_w2_w2 == "Does not apply" ~ 0,
      Q30_w2_w2 == "Applies somewhat" ~ 1,
      Q30_w2_w2 == "Applies well" ~ 2,
      Q30_w2_w2 == "1" ~ 0,
      Q30_w2_w2 == "2" ~1,
      Q30_w2_w2 == "3" ~ 2
    )
  )

#Hypocritical inparty 
final_drops <- final_drops %>% 
  mutate(hypocritical_inparty =
    case_when(
      Q18_w2_w2 == "Does not apply" ~ 0,
      Q18_w2_w2 == "Applies somewhat" ~ 1,
      Q18_w2_w2 == "Applies well" ~ 2,
      Q18_w2_w2 == "1" ~ 0,
      Q18_w2_w2 == "2" ~ 1,
      Q18_w2_w2 == "3" ~ 2
    )
  )


table(final_drops$Q18_w2_w2)

#Hypocritical outparty 
final_drops <- final_drops %>% 
  mutate(hypocritical_outparty =
    case_when(
      Q31_w2_w2 == "Does not apply" ~ 0,
      Q31_w2_w2 == "Applies somewhat" ~ 1,
      Q31_w2_w2 == "Applies well" ~ 2,
      Q31_w2_w2 == "1" ~ 0,
      Q31_w2_w2 == "2" ~ 1,
      Q31_w2_w2 == "3" ~ 2
    )
  )


#Selfish inparty
final_drops <- final_drops %>% 
  mutate(selfish_inparty =
    case_when(
      Q19_w2_w2 == "Does not apply" ~ 0,
      Q19_w2_w2 == "Applies somewhat" ~ 1,
      Q19_w2_w2 == "Applies well" ~ 2,
      Q19_w2_w2 == "1" ~ 0,
      Q19_w2_w2 == "2" ~ 1,
      Q19_w2_w2 == "3" ~ 2
    )
  )

#Selfish outparty
final_drops <- final_drops %>% 
  mutate(selfish_outparty =
    case_when(
      Q32_w2_w2 == "Does not apply" ~ 0,
      Q32_w2_w2 == "Applies somewhat" ~ 1,
      Q32_w2_w2 == "Applies well" ~ 2,
      Q32_w2_w2 == "1" ~ 0,
      Q32_w2_w2 == "2" ~ 1,
      Q32_w2_w2 == "3" ~ 2
    )
  )


#Mean inparty 
final_drops <- final_drops %>% 
  mutate(mean_inparty =
    case_when(
      Q20_w2_w2 == "Does not apply" ~ 0,
      Q20_w2_w2 == "Applies somewhat" ~ 1,
      Q20_w2_w2 == "Applies well" ~ 2,
      Q20_w2_w2 == "1" ~ 0,
      Q20_w2_w2 == "2" ~ 1,
      Q20_w2_w2 == "3" ~ 2
    )
  )

#Mean outparty
final_drops <- final_drops %>% 
  mutate(mean_outparty =
    case_when(
      Q33_w2_w2 == "Does not apply" ~ 0,
      Q33_w2_w2 == "Applies somewhat" ~ 1,
      Q33_w2_w2 == "Applies well" ~ 2,
      Q33_w2_w2 == "1" ~ 0,
      Q33_w2_w2 == "2" ~ 1,
      Q33_w2_w2 == "3" ~ 2
    )
  )

#check
table(is.na(final_drops$mean_outparty))

```

```{r stereo reverse coding}

#reverse coding negative stereotyping items
final_drops <- final_drops %>% 
  mutate(
    hypocritical_inparty_rec = 2 - hypocritical_inparty,
    hypocritical_outparty_rec = 2 - hypocritical_outparty, 
    selfish_inparty_rec = 2 - selfish_inparty,
    selfish_outparty_rec = 2 - selfish_outparty,
    mean_inparty_rec = 2 - mean_inparty,
    mean_outparty_rec = 2 - mean_outparty
  )


```


```{r stereo scaling}

#positive inparty stereotyping totals 
final_drops <- final_drops %>% 
  mutate(
    stereo_inparty_pos = 
      patriotic_inparty + 
      intelligent_inparty +
      honest_inparty +
      openminded_inparty +
      generous_inparty,
    stereo_inparty_pos_scale = 
      scale(stereo_inparty_pos)
  )

#check
summary(final_drops$stereo_inparty_pos_scale)

#outparty positive stereotyping total + scaled
final_drops <- final_drops %>% 
  mutate(
    stereo_outparty_pos = 
      patriotic_outparty + 
      intelligent_outparty +
      honest_outparty +
      openminded_outparty +
      generous_outparty,
    stereo_outparty_pos_scale = 
      scale(stereo_outparty_pos)
  )


#inparty negative stereotyping total + scaled
final_drops <- final_drops %>% 
  mutate(
    stereo_inparty_neg = 
      hypocritical_inparty + 
      selfish_inparty+
      mean_inparty,
    stereo_inparty_neg_scale = 
      scale(stereo_inparty_neg) 
  )


#outparty negative stereotyping + scaled
final_drops <- final_drops %>% 
  mutate(
    stereo_outparty_neg = 
      hypocritical_outparty + 
      selfish_outparty+
      mean_outparty,
    stereo_outparty_neg_scale = 
      scale(stereo_outparty_neg)
  )


#strict, 8-item stereotype inparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_inparty = 
      patriotic_inparty + 
      intelligent_inparty +
      honest_inparty +
      openminded_inparty +
      generous_inparty+
      hypocritical_inparty_rec +
      selfish_inparty_rec +
      mean_inparty_rec
  )

#check
summary(final_drops$stereo_inparty)


#strict, 8-item stereotype outparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_outparty = 
      patriotic_outparty + 
      intelligent_outparty +
      honest_outparty +
      openminded_outparty +
      generous_outparty+
      hypocritical_outparty_rec +
      selfish_outparty_rec +
      mean_outparty_rec
  )

#check
summary(final_drops$stereo_outparty)


#In minus Out stereotyping 
final_drops <- final_drops %>% 
  mutate(
    stereo_inout = stereo_inparty - stereo_outparty,
    stereo_inout_scale = scale(stereo_inout)
  )


```


```{r stereotyping w3}

table(final_drops$Q13_w3)

#Patriotic inparty
final_drops <- final_drops %>% 
  mutate(patriotic_inparty_w3 =
    case_when(
      Q13_w3 == "Does not apply" ~ 0,
      Q13_w3 == "Applies somewhat" ~ 1,
      Q13_w3 == "Applies well" ~ 2,
      Q13_w3 == "1" ~ 0,
      Q13_w3 == "2" ~ 1,
      Q13_w3 == "3" ~ 2
    )
  )

#check
table(is.na(final_drops$patriotic_inparty_w3))

#Patriotic outparty 
final_drops <- final_drops %>% 
  mutate(patriotic_outparty_w3 =
    case_when(
      Q26_w3 == "Does not apply" ~ 0,
      Q26_w3 == "Applies somewhat" ~ 1,
      Q26_w3 == "Applies well" ~ 2,
      Q26_w3 == "1" ~ 0,
      Q26_w3 == "2" ~ 1,
      Q26_w3 == "3" ~ 2
    )
  )

#check
table(final_drops$patriotic_outparty_w3)
  

#Intelligent inparty 
final_drops <- final_drops %>% 
  mutate(intelligent_inparty_w3 =
    case_when(
      Q14_w3 == "Does not apply" ~ 0,
      Q14_w3 == "Applies somewhat" ~ 1,
      Q14_w3 == "Applies well" ~ 2,
      Q14_w3 == "1" ~ 0,
      Q14_w3 == "2" ~ 1,
      Q14_w3 == "3" ~ 2
    )
  )

#check
table(is.na(final_drops$intelligent_inparty_w3))


#Intelligent outparty
final_drops <- final_drops %>% 
  mutate(intelligent_outparty_w3 =
    case_when(
      Q27_w3 == "Does not apply" ~ 0,
      Q27_w3 == "Applies somewhat" ~ 1,
      Q27_w3 == "Applies well" ~ 2,
      Q27_w3 == "1" ~ 0,
      Q27_w3 == "2" ~ 1,
      Q27_w3 == "3" ~ 2
    )
  )

#Honest inparty 
final_drops <- final_drops %>% 
  mutate(honest_inparty_w3 =
    case_when(
      Q15_w3 == "Does not apply" ~ 0,
      Q15_w3 == "Applies somewhat" ~ 1,
      Q15_w3 == "Applies well" ~ 2,
      Q15_w3 == "1" ~ 0,
      Q15_w3 == "2" ~ 1,
      Q15_w3 == "3" ~ 2
    )
  )

#Honest outparty
final_drops <- final_drops %>% 
  mutate(honest_outparty_w3 =
    case_when(
      Q28_w3 == "Does not apply" ~ 0,
      Q28_w3 == "Applies somewhat" ~ 1,
      Q28_w3 == "Applies well" ~ 2,
      Q28_w3 == "1" ~ 0,
      Q28_w3 == "2" ~ 1,
      Q28_w3 == "3" ~ 2
    )
  )


#Open minded inparty 
final_drops <- final_drops %>% 
  mutate(openminded_inparty_w3 =
    case_when(
      Q16_w3 == "Does not apply" ~ 0,
      Q16_w3 == "Applies somewhat" ~ 1,
      Q16_w3 == "Applies well" ~ 2,
      Q16_w3 == "1" ~ 0,
      Q16_w3 == "2" ~ 1,
      Q16_w3 == "3" ~ 2
    )
  )

#Open minded outparty 
final_drops <- final_drops %>% 
  mutate(openminded_outparty_w3 =
    case_when(
      Q29_w3 == "Does not apply" ~ 0,
      Q29_w3 == "Applies somewhat" ~ 1,
      Q29_w3 == "Applies well" ~ 2,
      Q29_w3 == "1" ~ 0,
      Q29_w3 == "2" ~ 1,
      Q29_w3 == "3" ~ 2
    )
  )


#Generous inparty
final_drops <- final_drops %>% 
  mutate(generous_inparty_w3 =
    case_when(
      Q17_w3 == "Does not apply" ~ 0,
      Q17_w3 == "Applies somewhat" ~ 1,
      Q17_w3 == "Applies well" ~ 2,
      Q17_w3 == "1" ~ 0,
      Q17_w3 == "2" ~ 1,
      Q17_w3 == "3" ~ 2
    )
  )

#Generous outparty
final_drops <- final_drops %>% 
  mutate(generous_outparty_w3 =
    case_when(
      Q30_w3 == "Does not apply" ~ 0,
      Q30_w3 == "Applies somewhat" ~ 1,
      Q30_w3 == "Applies well" ~ 2,
      Q30_w3 == "1" ~ 0,
      Q30_w3 == "2" ~ 1,
      Q30_w3 == "3" ~ 2
    )
  )

#Hypocritical inparty 
final_drops <- final_drops %>% 
  mutate(hypocritical_inparty_w3 =
    case_when(
      Q18_w3 == "Does not apply" ~ 0,
      Q18_w3 == "Applies somewhat" ~ 1,
      Q18_w3 == "Applies well" ~ 2,
      Q18_w3 == "1" ~ 0,
      Q18_w3 == "2" ~ 1,
      Q18_w3 == "3" ~ 2
    )
  )


#Hypocritical outparty 
final_drops <- final_drops %>% 
  mutate(hypocritical_outparty_w3 =
    case_when(
      Q31_w3 == "Does not apply" ~ 0,
      Q31_w3 == "Applies somewhat" ~ 1,
      Q31_w3 == "Applies well" ~ 2,
      Q31_w3 == "1" ~ 0,
      Q31_w3 == "2" ~ 1,
      Q31_w3 == "3" ~ 2
    )
  )


#Selfish inparty
final_drops <- final_drops %>% 
  mutate(selfish_inparty_w3 =
    case_when(
      Q19_w3 == "Does not apply" ~ 0,
      Q19_w3 == "Applies somewhat" ~ 1,
      Q19_w3 == "Applies well" ~ 2,
      Q19_w3 == "1" ~ 0,
      Q19_w3 == "2" ~ 1,
      Q19_w3 == "3" ~ 2
    )
  )

#Selfish outparty
final_drops <- final_drops %>% 
  mutate(selfish_outparty_w3 =
    case_when(
      Q32_w3 == "Does not apply" ~ 0,
      Q32_w3 == "Applies somewhat" ~ 1,
      Q32_w3 == "Applies well" ~ 2,
      Q32_w3 == "1" ~ 0,
      Q32_w3 == "2" ~ 1,
      Q32_w3 == "3" ~ 2
    )
  )


#Mean inparty 
final_drops <- final_drops %>% 
  mutate(mean_inparty_w3 =
    case_when(
      Q20_w3 == "Does not apply" ~ 0,
      Q20_w3 == "Applies somewhat" ~ 1,
      Q20_w3 == "Applies well" ~ 2,
      Q20_w3 == "1" ~ 0,
      Q20_w3 == "2" ~ 1,
      Q20_w3 == "3" ~ 2
    )
  )

#Mean outparty
final_drops <- final_drops %>% 
  mutate(mean_outparty_w3 =
    case_when(
      Q33_w3 == "Does not apply" ~ 0,
      Q33_w3 == "Applies somewhat" ~ 1,
      Q33_w3 == "Applies well" ~ 2,
      Q33_w3 == "1" ~ 0,
      Q33_w3 == "2" ~ 1,
      Q33_w3 == "3" ~ 2
    )
  )



```


```{r scaling W3}

#reverse coding
final_drops <- final_drops %>% 
  mutate(
    hypocritical_inparty_rec_w3 = 2 - hypocritical_inparty_w3,
    hypocritical_outparty_rec_w3 = 2 - hypocritical_outparty_w3, 
    selfish_inparty_rec_w3 = 2 - selfish_inparty_w3,
    selfish_outparty_rec_w3 = 2 - selfish_outparty_w3,
    mean_inparty_rec_w3 = 2 - mean_inparty_w3,
    mean_outparty_rec_w3 = 2 - mean_outparty_w3
  )



#strict, 8-item stereotype inparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_inparty_w3 = 
      patriotic_inparty_w3 + 
      intelligent_inparty_w3 +
      honest_inparty_w3 +
      openminded_inparty_w3 +
      generous_inparty_w3+
      hypocritical_inparty_rec_w3 +
      selfish_inparty_rec_w3 +
      mean_inparty_rec_w3
  )

#check
summary(final_drops$stereo_inparty_w3)


#strict, 8-item stereotype outparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_outparty_w3 = 
      patriotic_outparty_w3 + 
      intelligent_outparty_w3 +
      honest_outparty_w3 +
      openminded_outparty_w3 +
      generous_outparty_w3 +
      hypocritical_outparty_rec_w3 +
      selfish_outparty_rec_w3 +
      mean_outparty_rec_w3
  )

#check
summary(final_drops$stereo_outparty_w3)


#In minus Out stereotyping wave 3
final_drops <- final_drops %>% 
  mutate(
    stereo_inout_w3 = stereo_inparty_w3 - stereo_outparty_w3,
    stereo_inout_scale_w3 = scale(stereo_inout_w3)
  )

```


### Add-ons to stereotyping measures

```{r relabel}

#decent inparty
final_drops <- final_drops %>% 
  mutate(decent_inparty =
    case_when(
      Q21_w2_w2 == "Does not apply" ~ 0,
      Q21_w2_w2 == "Applies somewhat" ~ 1,
      Q21_w2_w2 == "Applies well" ~ 2,
      Q21_w2_w2 == "1" ~ 0,
      Q21_w2_w2 == "2" ~ 1,
      Q21_w2_w2 == "3" ~ 2
    )
  )

#decent outparty
final_drops <- final_drops %>% 
  mutate(decent_outparty =
    case_when(
      Q34_w2_w2 == "Does not apply" ~ 0,
      Q34_w2_w2 == "Applies somewhat" ~ 1,
      Q34_w2_w2 == "Applies well" ~ 2,
      Q34_w2_w2 == "1" ~ 0,
      Q34_w2_w2 == "2" ~ 1,
      Q34_w2_w2 == "3" ~ 2
    )
  )

#check
table(is.na(final_drops$decent_outparty))

#unethical inparty
final_drops <- final_drops %>% 
  mutate(unethical_inparty =
    case_when(
      Q22_w2_w2 == "Does not apply" ~ 0,
      Q22_w2_w2 == "Applies somewhat" ~ 1,
      Q22_w2_w2 == "Applies well" ~ 2,
      Q22_w2_w2 == "1" ~ 0,
      Q22_w2_w2 == "2" ~ 1,
      Q22_w2_w2 == "3" ~ 2
    )
  )


#unethical outparty
final_drops <- final_drops %>% 
  mutate(unethical_outparty =
    case_when(
      Q35_w2_w2 == "Does not apply" ~ 0,
      Q35_w2_w2 == "Applies somewhat" ~ 1,
      Q35_w2_w2 == "Applies well" ~ 2,
      Q35_w2_w2 == "1" ~ 0,
      Q35_w2_w2 == "2" ~ 1,
      Q35_w2_w2 == "3" ~ 2
    )
  )


#uncaring inparty
final_drops <- final_drops %>% 
  mutate(uncaring_inparty =
    case_when(
      Q24_w2_w2 == "Does not apply" ~ 0,
      Q24_w2_w2 == "Applies somewhat" ~ 1,
      Q24_w2_w2 == "Applies well" ~ 2,
      Q24_w2_w2 == "1" ~ 0,
      Q24_w2_w2 == "2" ~ 1,
      Q24_w2_w2 == "3" ~ 2
    )
  )

#uncaring outparty
final_drops <- final_drops %>% 
  mutate(uncaring_outparty =
    case_when(
      Q37_w2_w2 == "Does not apply" ~ 0,
      Q37_w2_w2 == "Applies somewhat" ~ 1,
      Q37_w2_w2 == "Applies well" ~ 2,
      Q37_w2_w2 == "1" ~ 0,
      Q37_w2_w2 == "2" ~ 1,
      Q37_w2_w2 == "3" ~ 2
    )
  )


```


```{r relabel wave 3}

#decent inparty w3
final_drops <- final_drops %>% 
  mutate(decent_inparty_w3 =
    case_when(
      Q21_w3 == "Does not apply" ~ 0,
      Q21_w3 == "Applies somewhat" ~ 1,
      Q21_w3 == "Applies well" ~ 2,
      Q21_w3 == "1" ~ 0,
      Q21_w3 == "2" ~ 1,
      Q21_w3 == "3" ~ 2
    )
  )

table(is.na(final_drops$decent_inparty_w3))

#decent outparty w3
final_drops <- final_drops %>% 
  mutate(decent_outparty_w3 =
    case_when(
      Q34_w3 == "Does not apply" ~ 0,
      Q34_w3 == "Applies somewhat" ~ 1,
      Q34_w3 == "Applies well" ~ 2,
      Q34_w3 == "1" ~ 0,
      Q34_w3 == "2" ~ 1,
      Q34_w3 == "3" ~ 2
    )
  )


#unethical inparty w3
final_drops <- final_drops %>% 
  mutate(unethical_inparty_w3 =
    case_when(
      Q22_w3 == "Does not apply" ~ 0,
      Q22_w3 == "Applies somewhat" ~ 1,
      Q22_w3 == "Applies well" ~ 2,
      Q22_w3 == "1" ~ 0,
      Q22_w3 == "2" ~ 1,
      Q22_w3 == "3" ~ 2
    )
  )


#unethical outparty w3
final_drops <- final_drops %>% 
  mutate(unethical_outparty_w3 =
    case_when(
      Q35_w3 == "Does not apply" ~ 0,
      Q35_w3 == "Applies somewhat" ~ 1,
      Q35_w3 == "Applies well" ~ 2,
      Q35_w3 == "1" ~ 0,
      Q35_w3 == "2" ~ 1,
      Q35_w3 == "3" ~ 2
    )
  )

#uncaring inparty w3
final_drops <- final_drops %>% 
  mutate(uncaring_inparty_w3 =
    case_when(
      Q24_w3 == "Does not apply" ~ 0,
      Q24_w3 == "Applies somewhat" ~ 1,
      Q24_w3 == "Applies well" ~ 2,
      Q24_w3 == "1" ~ 0,
      Q24_w3 == "2" ~ 1,
      Q24_w3 == "3" ~ 2
    )
  )

#uncaring outparty w3
final_drops <- final_drops %>% 
  mutate(uncaring_outparty_w3 =
    case_when(
      Q37_w3 == "Does not apply" ~ 0,
      Q37_w3 == "Applies somewhat" ~ 1,
      Q37_w3 == "Applies well" ~ 2,
      Q37_w3 == "1" ~ 0,
      Q37_w3 == "2" ~ 1,
      Q37_w3 == "3" ~ 2
    )
  )



```


```{r reverse code}

#reverse code negative items
final_drops <- final_drops %>% 
  mutate(
    unethical_inparty_rec = 2 -
      unethical_inparty,
    unethical_outparty_rec = 2 -
      unethical_outparty, 
    uncaring_inparty_rec = 2- 
      uncaring_inparty,
    uncaring_outparty_rec = 2-
      uncaring_outparty
  )

#reverse code w3 
final_drops <- final_drops %>% 
  mutate(
    unethical_inparty_rec_w3 = 2 -
      unethical_inparty_w3,
    unethical_outparty_rec_w3 = 2 -
      unethical_outparty_w3, 
    uncaring_inparty_rec_w3 = 2- 
      uncaring_inparty_w3,
    uncaring_outparty_rec_w3 = 2-
      uncaring_outparty_w3
  )

```

```{r scaling stereo score}

#11-item stereotype inparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_inparty_full = 
      patriotic_inparty + 
      intelligent_inparty +
      honest_inparty +
      openminded_inparty +
      generous_inparty+
      decent_inparty +
      hypocritical_inparty_rec +
      selfish_inparty_rec +
      mean_inparty_rec +
      unethical_inparty_rec +
      uncaring_inparty_rec
  )

#check
#summary(final_df$stereo_inparty_full)


#11-item stereotype outparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_outparty_full = 
      patriotic_outparty + 
      intelligent_outparty +
      honest_outparty +
      openminded_outparty +
      generous_outparty+
      decent_outparty +
      hypocritical_outparty_rec +
      selfish_outparty_rec +
      mean_outparty_rec +
      unethical_outparty_rec +
      uncaring_outparty_rec
  )

#check
#summary(final_df$stereo_outparty_full)


#In minus Out stereotyping 
final_drops <- final_drops %>% 
  mutate(
    stereo_inout_full = stereo_inparty_full - stereo_outparty_full,
    stereo_inout_full_scale = scale(stereo_inout_full)
  )

#summary(final_drops$stereo_inout)
#summary(final_drops$stereo_inout_full)

```

```{r wave 3 scaling stereo score}

#11-item stereotype inparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_inparty_full_w3 = 
      patriotic_inparty_w3 + 
      intelligent_inparty_w3 +
      honest_inparty_w3 +
      openminded_inparty_w3 +
      generous_inparty_w3+
      decent_inparty_w3 +
      hypocritical_inparty_rec_w3 +
      selfish_inparty_rec_w3 +
      mean_inparty_rec_w3 +
      unethical_inparty_rec_w3 +
      uncaring_inparty_rec_w3
  )

#check
#summary(final_drops$stereo_inparty_full)

#11-item stereotype outparty totals (with reverse-coding)
final_drops <- final_drops %>% 
  mutate(
    stereo_outparty_full_w3 = 
      patriotic_outparty_w3 + 
      intelligent_outparty_w3 +
      honest_outparty_w3 +
      openminded_outparty_w3 +
      generous_outparty_w3+
      decent_outparty_w3 +
      hypocritical_outparty_rec_w3 +
      selfish_outparty_rec_w3 +
      mean_outparty_rec_w3 +
      unethical_outparty_rec_w3 +
      uncaring_outparty_rec_w3
  )

#check
#summary(final_drops$stereo_outparty_full)


#In minus Out stereotyping 
final_drops <- final_drops %>% 
  mutate(
    stereo_inout_full_w3 = stereo_inparty_full_w3 - stereo_outparty_full_w3,
    stereo_inout_full_scale_w3 = scale(stereo_inout_full_w3)
  )

#summary(final_drops$stereo_inout)
#summary(final_drops$stereo_inout_full_scale_w3)

```


## Mass perceptions

```{r mass perceptions}

#name variables
final_drops <- final_drops %>% 
  mutate(
  mass_abortion_outparty = as.numeric(Q54_w2),
	mass_leave_outparty = as.numeric(Q55_w2),
	mass_marriage_outparty = as.numeric(Q56_w2),
	mass_abortion_inparty = as.numeric(Q57_w2),
	mass_leave_inparty = as.numeric(Q58_w2),
	mass_marriage_inparty = as.numeric(Q59_w2)
  )

#remove 997 and 998 values
final_drops <- final_drops %>%
  mutate( 
    mass_abortion_outparty = 
      replace(mass_abortion_outparty, mass_abortion_outparty > 100, NA),
    mass_leave_outparty = 
      replace(mass_leave_outparty, mass_leave_outparty > 100, NA),
    mass_marriage_outparty = 
      replace(mass_marriage_outparty, mass_marriage_outparty > 100, NA),
    mass_abortion_inparty = 
      replace(mass_abortion_inparty, mass_abortion_inparty > 100, NA),
    mass_leave_inparty = 
      replace(mass_leave_inparty, mass_leave_inparty > 100, NA),
    mass_marriage_inparty = 
      replace(mass_marriage_inparty,  mass_marriage_inparty > 100, NA),
  )

#difference
final_drops <- final_drops %>% 
  mutate(
    mass_abortion_diff = mass_abortion_inparty - mass_abortion_outparty,
    mass_leave_diff = mass_leave_inparty - mass_leave_outparty,
    mass_marriage_diff = mass_marriage_inparty - mass_marriage_outparty
  )


```


```{r mass perceptions w3}

#name variables
final_drops <- final_drops %>% 
  mutate(
  mass_abortion_outparty_w3 = as.numeric(Q54_w3),
	mass_leave_outparty_w3 = as.numeric(Q55_w3),
	mass_marriage_outparty_w3 = as.numeric(Q56_w3),
	mass_abortion_inparty_w3 = as.numeric(Q57_w3),
	mass_leave_inparty_w3 = as.numeric(Q58_w3),
	mass_marriage_inparty_w3 = as.numeric(Q59_w3)
  )

final_drops <- final_drops %>%
  mutate( 
    mass_abortion_outparty_w3 = 
      replace(mass_abortion_outparty_w3, mass_abortion_outparty_w3 > 100, NA),
    mass_leave_outparty_w3 = 
      replace(mass_leave_outparty_w3, mass_leave_outparty_w3 > 100, NA),
    mass_marriage_outparty_w3 = 
      replace(mass_marriage_outparty_w3, mass_marriage_outparty_w3 > 100, NA),
    mass_abortion_inparty_w3 = 
      replace(mass_abortion_inparty_w3, mass_abortion_inparty_w3 > 100, NA),
    mass_leave_inparty_w3 = 
      replace(mass_leave_inparty_w3, mass_leave_inparty_w3 > 100, NA),
    mass_marriage_inparty_w3 = 
      replace(mass_marriage_inparty_w3,  mass_marriage_inparty_w3 > 100, NA),
  )


#difference
final_drops <- final_drops %>% 
  mutate(
    mass_abortion_diff_w3 = mass_abortion_inparty_w3 - mass_abortion_outparty_w3,
    mass_leave_diff_w3 = mass_leave_inparty_w3 - mass_leave_outparty_w3,
    mass_marriage_diff_w3 = mass_marriage_inparty_w3 - mass_marriage_outparty_w3
  )

summary(final_drops$mass_abortion_diff_w3)

```


```{r mass perceptions index}

#create new variable 
final_drops <- final_drops %>% 
  mutate(
      mass_abort_scale = scale(mass_abortion_diff),
      mass_leave_scale = scale(mass_leave_diff),
      mass_marriage_scale = scale(mass_marriage_diff)
  )


#create keys 
keys_mass <- list(
  final_drops=cs(mass_abort_scale, mass_leave_scale, mass_marriage_scale)
)

#alpha function 
tmp_mass <- alpha(final_drops, keys_mass, check.keys = T, use ="pairwise")


#create indexed variable
final_drops <- final_drops %>% 
  mutate(
    mass_perc_idx = tmp_mass$scores
  )

#check (a little off, probably due to missing NAs from attrition)
summary(final_drops$mass_perc_idx)
summary(final_df$mass_perc_idx)


#remove vestiges
remove(keys_mass)
remove(tmp_mass)


```


```{r mass perceptions index w3}

#create new variable
final_drops <- final_drops %>% 
  mutate(
      mass_abort_scale_w3 = scale(mass_abortion_diff_w3),
      mass_leave_scale_w3 = scale(mass_leave_diff_w3),
      mass_marriage_scale_w3 = scale(mass_marriage_diff_w3)
  )


#create keys 
keys_mass_w3 <- list(
  final_drops=cs(mass_abort_scale_w3, mass_leave_scale_w3, mass_marriage_scale_w3)
)

#alpha function 
tmp_mass_w3 <- alpha(final_drops, keys_mass_w3, check.keys = T, use ="pairwise")


#create indexed variable
final_drops <- final_drops %>% 
  mutate(
    mass_perc_idx_w3 = tmp_mass_w3$scores
  )

#check 
summary(final_drops$mass_perc_idx_w3)

#remove vestiges
remove(tmp_mass_w3)
remove(keys_mass_w3)


```


## Yarrow Questions

```{r relationships negatively affected by polarization}

table(final_drops$Q41_w2_w2)

#polarization negatively affects relationships
final_drops <- final_drops %>% 
  mutate(
    neg_pol = #create variable
      case_when(
        Q41_w2_w2 == "Strongly disagree" ~ 0,
        Q41_w2_w2 == "Somewhat disagree" ~ 1,
        Q41_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q41_w2_w2 == "Somewhat agree" ~ 3,
        Q41_w2_w2 == "Strongly agree" ~ 4,
        Q41_w2_w2 == "1" ~ 0,
        Q41_w2_w2 == "2" ~ 1,
        Q41_w2_w2 == "3" ~ 2,
        Q41_w2_w2 == "4" ~ 3,
        Q41_w2_w2 == "5" ~ 4
      ), 
    neg_pol_dum = neg_pol > 2 #create dummy
  )

table(final_drops$neg_pol)

```


```{r repair polarized relationships}

table(final_drops$Q42_w2_w2)

#ability to repair polarized relationships 
final_drops <- final_drops %>% 
  mutate(
    repair_pol = #create variable
      case_when(
        Q42_w2_w2 == "Strongly disagree" ~ 0,
        Q42_w2_w2 == "Somewhat disagree" ~ 1,
        Q42_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q42_w2_w2 == "Somewhat agree" ~ 3,
        Q42_w2_w2 == "Strongly agree" ~ 4,
        Q42_w2_w2 == "1" ~ 0,
        Q42_w2_w2 == "2" ~ 1,
        Q42_w2_w2 == "3" ~ 2,
        Q42_w2_w2 == "4" ~ 3,
        Q42_w2_w2 == "5" ~ 4
      ), 
    repair_pol_dum = repair_pol > 2, #create dummy
    repair_pol_scale=scale(repair_pol)
    )


```


```{r curious}

#curiosity about outparty 
final_drops <- final_drops %>% 
  mutate(
    curious_outparty = #create variable
      case_when(
        Q43_w2_w2 == "Strongly disagree" ~ 0,
        Q43_w2_w2 == "Somewhat disagree" ~ 1,
        Q43_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q43_w2_w2 == "Somewhat agree" ~ 3,
        Q43_w2_w2 == "Strongly agree" ~ 4,
        Q43_w2_w2 == "1" ~ 0,
        Q43_w2_w2 == "2" ~ 1,
        Q43_w2_w2 == "3" ~ 2,
        Q43_w2_w2 == "4" ~ 3,
        Q43_w2_w2 == "5" ~ 4
      ), 
    curious_outparty_dum = curious_outparty > 2 #create dummy
  )


#check
table(is.na(final_drops$curious_outparty))

#Wave 3
final_drops <- final_drops %>% 
  mutate(
    curious_outparty_w3 = #create variable
      case_when(
        Q43_w3 == "Strongly disagree" ~ 0,
        Q43_w3 == "Somewhat disagree" ~ 1,
        Q43_w3 == "Neither agree nor disagree" ~ 2,
        Q43_w3 == "Somewhat agree" ~ 3,
        Q43_w3 == "Strongly agree" ~ 4,
        Q43_w3 == "1" ~ 0,
        Q43_w3 == "2" ~ 1,
        Q43_w3 == "3" ~ 2,
        Q43_w3 == "4" ~ 3,
        Q43_w3 == "5" ~ 4
      ), 
    curious_outparty_dum_w3 = curious_outparty_w3 > 2 #create dummy
  )

#scale
final_drops <- final_drops %>% 
  mutate(
    curious_outparty_scale = scale(curious_outparty),
    curious_outparty_w3_scale = scale(curious_outparty_w3)
  )


```

```{r optimism about polarization}

table(final_drops$Q47_w2)

#optimism about overcoming polarization (reverse-coded, in original 1 is extremely optimistic, 7 is ext pessimistic)
final_drops <- final_drops %>% 
  mutate(
    optimism_pol = #create variable
      case_when(
        Q47_w2 == "Extremely pessimistic" ~ 0,
        Q47_w2 == "Very pessimistic" ~ 1,
        Q47_w2 == "Somewhat pessimistic" ~ 2,
        Q47_w2 == "Neither optimistic nor pessimistic" ~ 3,
        Q47_w2 == "Somewhat optimistic" ~ 4,
        Q47_w2 == "Very optimistic" ~ 5,
        Q47_w2 == "Extremely optimistic" ~ 6,
        Q47_w2 == "1" ~ 6,
        Q47_w2 == "2" ~ 5,
        Q47_w2 == "3" ~ 4,
        Q47_w2 == "4" ~ 3,
        Q47_w2 == "5" ~ 2,
        Q47_w2 == "6" ~ 1,
        Q47_w2 == "7" ~ 0
      )
  )


#wave 3
#optimism about overcoming polarization
final_drops <- final_drops %>% 
  mutate(
    optimism_pol_w3 = #create variable
      case_when(
        Q47_w3 == "Extremely pessimistic" ~ 0,
        Q47_w3 == "Very pessimistic" ~ 1,
        Q47_w3 == "Somewhat pessimistic" ~ 2,
        Q47_w3 == "Neither optimistic nor pessimistic" ~ 3,
        Q47_w3 == "Somewhat optimistic" ~ 4,
        Q47_w3 == "Very optimistic" ~ 5,
        Q47_w3 == "Extremely optimistic" ~ 6,
        Q47_w3 == "1" ~ 6,
        Q47_w3 == "2" ~ 5,
        Q47_w3 == "3" ~ 4,
        Q47_w3 == "4" ~ 3,
        Q47_w3 == "5" ~ 2,
        Q47_w3 == "6" ~ 1,
        Q47_w3 == "7" ~ 0
      )
  )



```


```{r non-violent political change}


#belief in possibility of non-violent democratic change
final_drops <- final_drops %>% 
  mutate(
    nonviol_change = #create variable
      case_when(
        Q48_w2_w2 == "Strongly disagree" ~ 0,
        Q48_w2_w2 == "Somewhat disagree" ~ 1,
        Q48_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q48_w2_w2 == "Somewhat agree" ~ 3,
        Q48_w2_w2 == "Strongly agree" ~ 4,
        Q48_w2_w2 == "1" ~ 0,
        Q48_w2_w2 == "2" ~ 1,
        Q48_w2_w2 == "3" ~ 2,
        Q48_w2_w2 == "4" ~ 3,
        Q48_w2_w2 == "5" ~ 4
      )
  )

#wave 3
final_drops <- final_drops %>% 
  mutate(
    nonviol_change_w3 = #create variable
      case_when(
        Q48_w3 == "Strongly disagree" ~ 0,
        Q48_w3 == "Somewhat disagree" ~ 1,
        Q48_w3 == "Neither agree nor disagree" ~ 2,
        Q48_w3 == "Somewhat agree" ~ 3,
        Q48_w3 == "Strongly agree" ~ 4,
        Q48_w3 == "1" ~ 0,
        Q48_w3 == "2" ~ 1,
        Q48_w3 == "3" ~ 2,
        Q48_w3 == "4" ~ 3,
        Q48_w3 == "5" ~ 4
      )
  )


#check
table(is.na(final_drops$nonviol_change))

#scale
final_drops <- final_drops %>% 
  mutate(
    nonviol_change_scale = scale(nonviol_change),
    nonviol_change_scale_w3 = scale(nonviol_change_w3)
  )

```

```{r optimism about restoring civility}

#optimism about restoring civility
final_drops <- final_drops %>% 
  mutate(
    optimism_civil = #create variable
      case_when(
        Q49_w2_w2 == "Strongly disagree" ~ 0,
        Q49_w2_w2 == "Somewhat disagree" ~ 1,
        Q49_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q49_w2_w2 == "Somewhat agree" ~ 3,
        Q49_w2_w2 == "Strongly agree" ~ 4,
        Q49_w2_w2 == "1" ~ 0,
        Q49_w2_w2 == "2" ~ 1,
        Q49_w2_w2 == "3" ~ 2,
        Q49_w2_w2 == "4" ~ 3,
        Q49_w2_w2 == "5" ~ 4
      )
  )


#wave 3
final_drops <- final_drops %>% 
  mutate(
    optimism_civil_w3 = #create variable
      case_when(
        Q49_w3 == "Strongly disagree" ~ 0,
        Q49_w3 == "Somewhat disagree" ~ 1,
        Q49_w3 == "Neither agree nor disagree" ~ 2,
        Q49_w3 == "Somewhat agree" ~ 3,
        Q49_w3 == "Strongly agree" ~ 4,
        Q49_w3 == "1" ~ 0,
        Q49_w3 == "2" ~ 1,
        Q49_w3 == "3" ~ 2,
        Q49_w3 == "4" ~ 3,
        Q49_w3 == "5" ~ 4
      )
  )

```

```{r optimism about survival of democratic institutions}

#optimism about survival of democratic institutions
final_drops <- final_drops %>% 
  mutate(
    optimism_survive = #create variable
      case_when(
        Q50_w2_w2 == "Strongly disagree" ~ 0,
        Q50_w2_w2 == "Somewhat disagree" ~ 1,
        Q50_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q50_w2_w2 == "Somewhat agree" ~ 3,
        Q50_w2_w2 == "Strongly agree" ~ 4, 
        Q50_w2_w2 == "1" ~ 0,
        Q50_w2_w2 == "2" ~ 1,
        Q50_w2_w2 == "3" ~ 2,
        Q50_w2_w2 == "4" ~ 3,
        Q50_w2_w2 == "5" ~ 4
      )
  )

#wave 3
final_drops <- final_drops %>% 
  mutate(
    optimism_survive_w3 = #create variable
      case_when(
        Q50_w3 == "Strongly disagree" ~ 0,
        Q50_w3 == "Somewhat disagree" ~ 1,
        Q50_w3 == "Neither agree nor disagree" ~ 2,
        Q50_w3 == "Somewhat agree" ~ 3,
        Q50_w3 == "Strongly agree" ~ 4,
        Q50_w3 == "1" ~ 0,
        Q50_w3 == "2" ~ 1,
        Q50_w3 == "3" ~ 2,
        Q50_w3 == "4" ~ 3,
        Q50_w3 == "5" ~ 4
      )
  )


```

```{r optimism scale}

#optimism about: (1) survival of democratic institutions + (2) restoring civility + (3) overcoming polarizatinon

final_drops <- final_drops %>% 
  mutate(
    optimism_survive_scale = scale(optimism_survive),
    optimism_civil_scale = scale(optimism_civil),
    optimism_pol_scale = scale(optimism_pol),
    optimism_total = scale(optimism_survive + 
                             optimism_civil + 
                             optimism_pol)
)

summary(final_drops$optimism_total)

#wave 3 
final_drops <- final_drops %>% 
  mutate(
    optimism_survive_scale_w3 = scale(optimism_survive_w3),
    optimism_civil_scale_w3 = scale(optimism_civil_w3),
    optimism_pol_scale_w3 = scale(optimism_pol_w3),
    optimism_total_w3 = 
      scale(optimism_survive_w3 +
              optimism_civil_w3 + 
              optimism_pol_w3)
  )


```


```{r rebuild trust}

#time to rebuild trust across political divide
#0 "Never" 1 "Over 25 years" 2 "10-20 years" 3 "5-10 years" 4 "0-5 years"

final_drops <- final_drops %>% 
  mutate(
    rebuild_trust = #create variable
      case_when(
        Q51_w2 == "Never" ~ 0,
        Q51_w2 == "Over 25 years" ~ 1,
        Q51_w2 == "10-20 years" ~ 2,
        Q51_w2 == "5-10 years" ~ 3,
        Q51_w2 == "0-5 years" ~ 4,
        Q51_w2 == "5" ~ 0,
        Q51_w2 == "4" ~ 1,
        Q51_w2 == "3" ~ 2,
        Q51_w2 == "2" ~ 3,
        Q51_w2 == "1" ~ 4,
      ),
    rebuild_trust_scale = scale(rebuild_trust)
  )

#check
table(is.na(final_drops$rebuild_trust))

#wave 3 
final_drops <- final_drops %>% 
  mutate(
    rebuild_trust_w3 = #create variable
      case_when(
        Q51_w3 == "Never" ~ 0,
        Q51_w3 == "Over 25 years" ~ 1,
        Q51_w3 == "10-20 years" ~ 2,
        Q51_w3 == "5-10 years" ~ 3,
        Q51_w3 == "0-5 years" ~ 4,
        Q51_w3 == "5" ~ 0,
        Q51_w3 == "4" ~ 1,
        Q51_w3 == "3" ~ 2,
        Q51_w3 == "2" ~ 3,
        Q51_w3 == "1" ~ 4,
      ),
    rebuild_trust_scale_w3 = scale(rebuild_trust_w3)
  )



```

```{r unity}

#perception of unity among ordinary Americans 
final_drops <- final_drops %>% 
  mutate(
    unity = as.numeric(Q52_w2),
    unity = replace(unity, unity >100, NA),
    unity_scale = scale(unity)
  )

#check
summary(final_drops$unity_scale)
table(is.na(final_drops$unity_scale))


#wave 3 
final_drops <- final_drops %>% 
  mutate(
    unity_scale_w3 = as.numeric(Q52_w3),
    unity_scale_w3 = replace(unity_scale_w3, unity_scale_w3 >100, NA),
    unity_scale_w3 = scale(unity_scale_w3)
  )

```

```{r belief in dialogue}

#belief in dialogue 
final_drops <- final_drops %>% 
  mutate(
    dialogue = as.numeric(Q53_w2),
    dialogue = replace(dialogue, dialogue >100, NA),
    dialogue_scale = scale(dialogue)
  )

#wave 3 
final_drops <- final_drops %>% 
  mutate(
    dialogue_scale_w3 = as.numeric(Q53_w3),
    dialogue_scale_w3 = replace(dialogue_scale_w3, dialogue_scale_w3 >100, NA),
    dialogue_scale_w3 = scale(dialogue_scale_w3)
  )

```


## Partisan Understanding / Open-Mindedness

How well do the following statements describe you? 

```{r partisan understanding}

#partisan understanding 1: I sometimes find it difficult to see things from a OUTPARTY’s point of view
final_drops <- final_drops %>% 
  mutate(
    understand1 = 
      case_when(
        Q39_w2_w2 == "Extremely well" ~ 0, 
        Q39_w2_w2 == "Very well" ~ 1,
        Q39_w2_w2 == "Moderately well" ~ 2,
        Q39_w2_w2 == "Slightly well" ~ 3,
        Q39_w2_w2 == "Not well at all" ~ 4,
        Q39_w2_w2 == "5" ~ 0, 
        Q39_w2_w2 == "4" ~ 1,
        Q39_w2_w2 == "3" ~ 2,
        Q39_w2_w2 == "2" ~ 3,
        Q39_w2_w2 == "1" ~ 4
      )
  )

#check
table(is.na(final_drops$understand1))

#wave 3
final_drops <- final_drops %>% 
  mutate(
    understand1_w3 = 
      case_when(
        Q39_w3 == "Extremely well" ~ 0, 
        Q39_w3 == "Very well" ~ 1,
        Q39_w3 == "Moderately well" ~ 2,
        Q39_w3 == "Slightly well" ~ 3,
        Q39_w3 == "Not well at all" ~ 4,
        Q39_w3 == "5" ~ 0, 
        Q39_w3 == "4" ~ 1,
        Q39_w3 == "3" ~ 2,
        Q39_w3 == "2" ~ 3,
        Q39_w3 == "1" ~ 4
      )
  )

#partisan understanding 2: I sometimes try to understand OUTPARTYs better by imagining how things look from their perspective
final_drops <- final_drops %>% 
  mutate(
    understand2 = 
      case_when(
        Q40_w2_w2 == "Extremely well" ~ 4, 
        Q40_w2_w2 == "Very well" ~ 3,
        Q40_w2_w2 == "Moderately well" ~ 2,
        Q40_w2_w2 == "Slightly well" ~ 1,
        Q40_w2_w2 == "Not well at all" ~ 0,
        Q40_w2_w2 == "5" ~ 0, 
        Q40_w2_w2 == "4" ~ 1,
        Q40_w2_w2 == "3" ~ 2,
        Q40_w2_w2 == "2" ~ 3,
        Q40_w2_w2 == "1" ~ 4
      )
  )

#wave 3
final_drops <- final_drops %>% 
  mutate(
    understand2_w3 = 
      case_when(
        Q40_w3 == "Extremely well" ~ 4, 
        Q40_w3 == "Very well" ~ 3,
        Q40_w3 == "Moderately well" ~ 2,
        Q40_w3 == "Slightly well" ~ 1,
        Q40_w3 == "Not well at all" ~ 0,
        Q40_w3 == "5" ~ 0, 
        Q40_w3 == "4" ~ 1,
        Q40_w3 == "3" ~ 2,
        Q40_w3 == "2" ~ 3,
        Q40_w3 == "1" ~ 4
      )
  )

#combined measure (scaled)
final_drops <- final_drops %>% 
  mutate(
    understand1_rev = rev(understand1),
    understand1_w3_rev = rev(understand1_w3),
    understand_total = scale(understand1_rev+understand2),
    understand_total_w3 = scale(understand1_w3_rev+understand2_w3)
  )

```

Q45_w2_w2 - It is not worth my time trying to listen OUTPARTY
to talk about politics
Q46_w2_w2 - People should listen to concerns of party.

```{r Empathy / Tolerance}
#clean tolerance 
final_drops <- final_drops %>% 
  mutate(
    empathy1 = case_when( #reverse code it... 
        Q45_w2_w2 == "Strongly disagree" ~ 0,
        Q45_w2_w2 == "Somewhat disagree" ~ 1,
        Q45_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q45_w2_w2 == "Somewhat agree" ~ 3,
        Q45_w2_w2 == "Strongly agree" ~ 4, 
        Q45_w2_w2 == "1" ~ 0,
        Q45_w2_w2 == "2" ~ 1,
        Q45_w2_w2 == "3" ~ 2,
        Q45_w2_w2 == "4" ~ 3,
        Q45_w2_w2 == "5" ~ 4
  ),
     empathy2 = case_when(
        Q46_w2_w2 == "Strongly disagree" ~ 0,
        Q46_w2_w2 == "Somewhat disagree" ~ 1,
        Q46_w2_w2 == "Neither agree nor disagree" ~ 2,
        Q46_w2_w2 == "Somewhat agree" ~ 3,
        Q46_w2_w2 == "Strongly agree" ~ 4, 
        Q46_w2_w2 == "1" ~ 0,
        Q46_w2_w2 == "2" ~ 1,
        Q46_w2_w2 == "3" ~ 2,
        Q46_w2_w2 == "4" ~ 3,
        Q46_w2_w2 == "5" ~ 4
  ))

#reverse code empathy 1
final_drops <- final_drops %>% 
  mutate(
    empathy1_rev = rev(empathy1)
  )

#index 
final_drops <- final_drops %>% 
  mutate(
    empathy_scale = scale(empathy1_rev+empathy2)
  )


```



## Demographics / Covariates


```{r demogrpahics / covariates}

table(is.na(final_drops$birthyr))
check <- intersect(final_drops$birthyr, final_drops$birthyr.y)

#prepare birthyear 
final_drops <- final_drops %>% 
  mutate(
  birthyr = coalesce(birthyr,birthyr.x, birthyr.y), #birth year
)


#demographics
final_drops <- final_drops %>% 
  mutate(
    age = 2022 - birthyr, #age at the time taking the survey
    reg_vote = #voter reg
      case_when(
        votereg == "Yes" ~ 1,
        votereg == "No" ~ 0,
        votereg.x == "1" ~ 1,
        votereg.x == "2" ~ 0,
        votereg.y == "1" ~ 1,
        votereg.y == "2" ~ 0,
      ),
    sex = #gender 
      case_when(
        gender == "Female" ~ 1,
        gender == "Male" ~ 0,
        gender.x == "2" ~ 1, 
        gender.x == "1" ~ 0, 
        gender.y == "2" ~ 1,
        gender.y == "1" ~ 0
      ),
    race_2 = #race
      as.factor(case_when(
        race == "White" ~ 0,
        race == "Black" ~ 1,
        race == "Hispanic" ~ 2,
        race == "Other" ~ 3,
        race == "Asian" ~ 4,
        race == "Two or more races" ~ 5, 
        race == "Native American" ~ 6,
        race == "Middle Eastern" ~ 7, 
        race.x == "1" ~ 0,
        race.x == "2" ~ 1,
        race.x == "3" ~ 2,
        race.x == "7" ~ 3,
        race.x == "4" ~ 4,
        race.x == "6" ~ 5, 
        race.x == "5" ~ 6,
        race.x == "8" ~ 7, 
        race.y == "1" ~ 0,
        race.y == "2" ~ 1,
        race.y == "3" ~ 2,
        race.y == "7" ~ 3,
        race.y == "4" ~ 4,
        race.y == "6" ~ 5, 
        race.y == "5" ~ 6,
        race.y == "8" ~ 7, 
      )),
    white = # white vs. non-white 
      if_else(
        race_2 == 0, 1, 0
        ),
    region_2 = #region
      as.factor(case_when(
        region == "West" ~ 0,
        region == "Midwest" ~ 1, 
        region == "Northeast" ~ 2,
        region == "South" ~ 3,
        region.x == "4" ~ 0,
        region.x == "2" ~ 1,
        region.x == "1" ~ 2,
        region.x == "3" ~ 3,
        region.y == "4" ~ 0,
        region.y == "2" ~ 1,
        region.y == "1" ~ 2,
        region.y == "3" ~ 3
      )),
    educ_2 = #education 
      as.factor(case_when(
        educ == "No HS" ~ 0, 
        educ == "High school graduate" ~1,
        educ == "Some college" ~ 2,
        educ == "2-year" ~ 3, 
        educ == "4-year" ~ 4, 
        educ == "Post-grad" ~ 5,
        educ.x == "1" ~ 0, 
        educ.x == "2" ~ 1,
        educ.x == "3" ~ 2,
        educ.x == "4" ~ 3, 
        educ.x == "5" ~ 4, 
        educ.x == "6" ~ 5,
        educ.y == "1" ~ 0, 
        educ.y == "2" ~ 1,
        educ.y == "3" ~ 2,
        educ.y == "4" ~ 3, 
        educ.y == "5" ~ 4, 
        educ.y == "6" ~ 5
      )),
    educ_dum = #education 4-year college+ or not.
      case_when(
        educ_2 == 0 ~ 0, 
        educ_2 == 1 ~ 0,
        educ_2 == 2 ~ 0,
        educ_2 == 3 ~ 0, 
        educ_2 == 4 ~ 1, 
        educ_2 == 5 ~ 1
      ),
    marr = #married status 
      as.factor(case_when(
        marstat == "Married" ~ 0,
        marstat == "Domestic / civil partnership"  ~ 1,
        marstat == "Never married" ~ 2, 
        marstat == "Divorced" ~ 3, 
        marstat == "Widowed" ~ 4, 
        marstat == "Separated" ~ 5,
        marstat.x == "1" ~ 0,
        marstat.x == "6" ~ 1,
        marstat.x == "5" ~ 2, 
        marstat.x == "3" ~ 3, 
        marstat.x == "4" ~ 4, 
        marstat.x == "2" ~ 5,
        marstat.y == "1" ~ 0,
        marstat.y == "6" ~ 1,
        marstat.y == "5" ~ 2, 
        marstat.y == "3" ~ 3, 
        marstat.y == "4" ~ 4, 
        marstat.y == "2" ~ 5
      )),
    marr_dum = #currently married dummy
      case_when(
        marr == 0 ~ 1,
        marr == 1 ~ 1,
        marr == 2 ~ 0, 
        marr == 3 ~ 0, 
        marr == 4 ~ 0, 
        marr == 5 ~ 0
      ),
    income = #income 
      as.factor(case_when(
        faminc_new == "Less than $10,000" ~ 0,
        faminc_new == "$10,000 - $19,999" ~ 1,
        faminc_new == "$20,000 - $29,999" ~ 2,
        faminc_new == "$30,000 - $39,999" ~ 3,
        faminc_new == "$40,000 - $49,999" ~ 4,
        faminc_new == "$50,000 - $59,999" ~ 5,
        faminc_new == "$60,000 - $69,999" ~ 6,
        faminc_new == "$70,000 - $79,999" ~ 7,
        faminc_new == "$80,000 - $99,999" ~ 8,
        faminc_new == "$100,000 - $119,999" ~ 9,
        faminc_new == "$120,000 - $149,999" ~ 10,
        faminc_new == "$150,000 - $199,999" ~ 11,
        faminc_new == "$200,000 - $249,999" ~ 12,
        faminc_new == "$250,000 - $349,999" ~ 13,
        faminc_new == "$350,000 - $499,999" ~ 14,
        faminc_new == "$500,000 or more" ~ 15, 
        faminc_new.x == "1" ~ 0,
        faminc_new.x == "2" ~ 1,
        faminc_new.x == "3" ~ 2,
        faminc_new.x == "4" ~ 3,
        faminc_new.x == "5" ~ 4,
        faminc_new.x == "6" ~ 5,
        faminc_new.x == "7" ~ 6,
        faminc_new.x == "8" ~ 7,
        faminc_new.x == "9" ~ 8,
        faminc_new.x == "10" ~ 9,
        faminc_new.x == "11" ~ 10,
        faminc_new.x == "12" ~ 11,
        faminc_new.x == "13" ~ 12,
        faminc_new.x == "14" ~ 13,
        faminc_new.x == "15" ~ 14,
        faminc_new.x == "16" ~ 15,
        faminc_new.y == "1" ~ 0,
        faminc_new.y == "2" ~ 1,
        faminc_new.y == "3" ~ 2,
        faminc_new.y == "4" ~ 3,
        faminc_new.y == "5" ~ 4,
        faminc_new.y == "6" ~ 5,
        faminc_new.y == "7" ~ 6,
        faminc_new.y == "8" ~ 7,
        faminc_new.y == "9" ~ 8,
        faminc_new.y == "10" ~ 9,
        faminc_new.y == "11" ~ 10,
        faminc_new.y == "12" ~ 11,
        faminc_new.y == "13" ~ 12,
        faminc_new.y == "14" ~ 13,
        faminc_new.y == "15" ~ 14,
        faminc_new.y == "16" ~ 15,
      )),
        income_2 = #income 2, fewer categories (0-50k, 50k-100k, 100k-250k, >250k)
      as.factor(case_when(
        income == 0 ~ 0,
        income == 1 ~ 0,
        income == 2 ~ 0,
        income == 3 ~ 0,
        income == 4 ~ 0,
        income == 5 ~ 1,
        income == 6 ~ 1,
        income == 7 ~ 1,
        income == 8 ~ 1,
        income == 9 ~ 2,
        income == 10 ~ 2,
        income == 11 ~ 2,
        income == 12 ~ 2,
        income == 13 ~ 3,
        income == 14 ~ 3,
        income == 15 ~ 3
      )),
    job = #employment
      case_when(
        employ == "Full-time" ~ 1, 
        employ == "Part-time" ~ 1, 
        employ == "Retired" ~ 0,
        employ == "Homemaker" ~ 0,
        employ == "Student" ~ 0,
        employ == "Permanently disabled" ~ 0, 
        employ == "Unemployed" ~ 0, 
        employ == "Other" ~ 0, 
        employ == "Temporarily laid off" ~ 0,
        employ.x == "1" ~ 1, 
        employ.x == "2" ~ 1, 
        employ.x == "5" ~ 0,
        employ.x == "7" ~ 0,
        employ.x == "8" ~ 0,
        employ.x == "6" ~ 0, 
        employ.x == "4" ~ 0, 
        employ.x == "9" ~ 0, 
        employ.x == "3" ~ 0,
        employ.y == "1" ~ 1, 
        employ.y == "2" ~ 1, 
        employ.y == "5" ~ 0,
        employ.y == "7" ~ 0,
        employ.y == "8" ~ 0,
        employ.y == "6" ~ 0, 
        employ.y == "4" ~ 0, 
        employ.y == "9" ~ 0, 
        employ.y == "3" ~ 0,
        employ_t == "Full time job plus part time job" ~ 1, 
        employ_t == "Part time student work fulltime" ~ 1, 
        employ_t == "Permanently disabled and work part time" ~ 1, 
        employ_t == "Retired but working very hard anyway" ~ 0,
        employ_t == "self employed" ~ 1,
        employ_t == "Self employed" ~ 1, 
        employ_t == "Student and part time working" ~ 1
      ),
    child = #child under 18 in household
      case_when(
        child18 == "Yes" ~ 1, 
        child18 == "No" ~ 0,
        child18.x == "1" ~ 1,
        child18.x == "2" ~ 0,
        child18.y == "1" ~ 1,
        child18.y == "2" ~ 0
      ), 
    relig = #religion 
      case_when(
        religpew == "Protestant" ~ 1,
        religpew == "Roman Catholic" ~ 2,
        religpew == "Mormon" ~3,
        religpew == "Eastern or Greek Orthodox" ~4,
        religpew == "Jewish" ~5,
        religpew == "Muslim" ~6,
        religpew == "Buddhist" ~7,
        religpew == "Hindu" ~8,
        religpew == "Atheist" ~9,
        religpew == "Agnostic" ~10,
        religpew == "Nothing in particular" ~11,
        religpew == "Something else" ~12,
        religpew.x == "1" ~ 1,
        religpew.x == "2" ~2,
        religpew.x == "3" ~3,
        religpew.x == "4" ~4,
        religpew.x == "5" ~5,
        religpew.x == "6" ~6,
        religpew.x == "7" ~7,
        religpew.x == "8" ~8,
        religpew.x == "9" ~9,
        religpew.x == "10" ~10,
        religpew.x == "11" ~11,
        religpew.x == "12" ~12,
        religpew.y == "1" ~ 1,
        religpew.y == "2" ~2,
        religpew.y == "3" ~3,
        religpew.y == "4" ~4,
        religpew.y == "5" ~5,
        religpew.y == "6" ~6,
        religpew.y == "7" ~7,
        religpew.y == "8" ~8,
        religpew.y == "9" ~9,
        religpew.y == "10" ~10,
        religpew.y == "11" ~11,
        religpew.y == "12" ~12,
        religpew == "skipped" ~ NA_real_,
        religpew.x == "98" ~ NA_real_
        #religpew_t != "__NA__" ~ 11,
      ),
    christian = #religion, christian or not dummy
        case_when(
        religpew_t == "Apostolic pentecostal" ~ 1,
        religpew_t == "Apostolic/ Pentecostal" ~ 1,
        religpew_t == "Baptist" ~ 1, 
        religpew_t == "Baptist Christian" ~ 1, 
        religpew_t == "Christ" ~ 1,
        religpew_t == "christain" ~ 1,
        religpew_t == "Christen" ~ 1,
        religpew_t == "Christian" ~ 1,
        religpew_t == "Christian, Non-Denominational" ~ 1,
        religpew_t == "Jehovahs witnesses" ~ 1, 
        religpew_t == "Episcopal" ~ 1, 
        religpew_t == "Greek Catholic" ~ 1, 
        religpew_t == "I have a relationship with Jesus christ" ~ 1,
        religpew_t == "Non Denominational Christian" ~ 1,
        religpew_t == "Spiritual and a Christian" ~ 1,
        religpew_t == "Non denominational" ~1, 
        religpew_t == "Canthiest" ~ 0, 
        religpew_t == "I believe in a higher power. I believe in energy." ~ 0,
        religpew_t == "Muslim" ~ 0,
        religpew_t == "Neopagan with druidic roots" ~ 0,
        religpew_t == "Nihilist" ~ 0,
        religpew_t == "Norse pagen" ~ 0,
        religpew_t == "Unitarian Universalist" ~ 0, 
        religpew_t == "Wiccan" ~ 0, 
        religpew_t == "wiccian"~0,
        relig == 1 ~ 1, #Protestant
        relig == 2 ~ 1, #Roman Catholic 
        relig == 3 ~ 0, 
        relig == 4 ~ 1, #Eastern or Greek Orthodox 
        relig == 5 ~ 0,
        relig == 6 ~ 0,
        relig == 7 ~ 0,
        relig == 8 ~ 0,
        relig == 9 ~ 0,
        relig == 10 ~ 0,
        relig == 11 ~ 0,
        relig == 12 ~ 0
      ),
    vote_2020 = #2020 presidential vote 
      as.factor(case_when(
        presvote20post == "Joe Biden" ~ 0,
        presvote20post == "Donald Trump" ~ 1,
        presvote20post == "Did not vote for President" ~ 2,
        presvote20post == "Howie Hawkins" ~ 2,
        presvote20post == "Jo Jorgensen" ~ 2,
        presvote20post == "Other" ~ 2,
        presvote20post.x == "1" ~ 0,
        presvote20post.x == "2" ~ 1,
        presvote20post.x == "6" ~ 2,
        presvote20post.x == "4" ~ 2,
        presvote20post.x == "3" ~ 2,
        presvote20post.x == "5" ~ 2,
        presvote20post.y == "1" ~ 0,
        presvote20post.y == "2" ~ 1,
        presvote20post.y == "6" ~ 2,
        presvote20post.y == "4" ~ 2,
        presvote20post.y == "3" ~ 2,
        presvote20post.y == "5" ~ 2,
        presvote20post_t != "__NA__" ~ 2
      )),
    vote_2016 = #2016 presidential vote 
      as.factor(case_when(
        presvote16post == "Did not vote for President" ~ 2,
        presvote16post == "Hillary Clinton" ~ 0,
        presvote16post == "Donald Trump" ~ 1,
        presvote16post == "Gary Johnson" ~ 2,
        presvote16post == "Jill Stein" ~ 2,
        presvote16post == "Evan McMullin" ~ 2,
        presvote16post == "Other" ~ 2,
        presvote16post.x == "7" ~ 2,
        presvote16post.x == "1" ~ 0,
        presvote16post.x == "2" ~ 1,
        presvote16post.x == "3" ~ 2,
        presvote16post.x == "4" ~ 2,
        presvote16post.x == "5" ~ 2,
        presvote16post.x == "6" ~ 2,
        presvote16post.y == "7" ~ 2,
        presvote16post.y == "1" ~ 0,
        presvote16post.y == "2" ~ 1,
        presvote16post.y == "3" ~ 2,
        presvote16post.y == "4" ~ 2,
        presvote16post.y == "5" ~ 2,
        presvote16post.y == "6" ~ 2,
        presvote16post_t == "Hillary Clinton" ~ 0,
        presvote16post_t != "__NA__" ~ 2
      )),
    ideology_yg = #ideology
      case_when(
        ideo5 == "Not sure" ~ 0,
        ideo5 == "Very liberal" ~ 1,
        ideo5 == "Liberal" ~ 2,
        ideo5 == "Moderate" ~ 3,
        ideo5 == "Conservative" ~ 4,
        ideo5 == "Very conservative" ~ 5,
        ideo5.x == "6" ~ 0,
        ideo5.x == "1" ~ 1,
        ideo5.x == "2" ~ 2,
        ideo5.x == "3" ~ 3,
        ideo5.x == "4" ~ 4,
        ideo5.x == "5" ~ 5,
        ideo5.y == "6" ~ 0,
        ideo5.y == "1" ~ 1,
        ideo5.y == "2" ~ 2,
        ideo5.y == "3" ~ 3,
        ideo5.y == "4" ~ 4,
        ideo5.y == "5" ~ 5
      ),
    news_interest = #interest in news/politics
      case_when(
        newsint == "Hardly at all" ~ 0,
        newsint == "Only now and then" ~ 1,
        newsint == "Some of the time" ~ 2,
        newsint == "Most of the time" ~ 3,
        newsint.x == "4" ~ 0,
        newsint.x == "3" ~ 1,
        newsint.x == "2" ~ 2,
        newsint.x == "1" ~ 3,
        newsint.y == "4" ~ 0,
        newsint.y == "3" ~ 1,
        newsint.y == "2" ~ 2,
        newsint.y == "1" ~ 3
      ),
    turnout2020 = #2020 turnout 
      case_when(
        turnout20post == "Yes" ~ 1,
        turnout20post == "No" ~ 0,
        turnout20post.x == "1" ~ 1,
        turnout20post.x == "2" ~ 0,
        turnout20post.y == "1" ~ 1,
        turnout20post.y == "2" ~ 0
  )
)

```

#### Impute missing demographic values

```{r demographics impute missing values}

#list of covariates 
complete_covars <- paste("partyID", "ideology_yg", "age", "sex", "white", "educ_dum", "region_2",  sep = "+")

all_covars <- c("marr_dum", "christian", "child", "turnout2020", "job", "partyID", "ideology_yg", "age", "sex", "white", "educ_dum", "region_2")

#list of missing covariates
missing_covars <- c("marr_dum", "christian", "child", "turnout2020", "job")


#check participants
final_drops <- final_drops %>% 
  mutate(missers = 
           if_else(is.na(marr_dum), 1, 0) &
           if_else(is.na(christian), 1, 0) &
           if_else(is.na(child), 1, 0) &
           if_else(is.na(turnout2020), 1, 0) &
           if_else(is.na(job), 1, 0) 
           ,
         missers_any =
           if_else(is.na(marr_dum), 1, 0) |
           if_else(is.na(christian), 1, 0) |
           if_else(is.na(child), 1, 0) |
           if_else(is.na(turnout2020), 1, 0) |
           if_else(is.na(job), 1, 0) 
         )

#there are 17 participants who are chronic missers (for both clean and drops sample)
table(final_drops$missers)
table(final_drops$missers_any) #49/51 (clean/drops)


####Using MICE ####

#create dataframe for imputation + caseid 
covar_df <- final_drops %>% 
  dplyr::select(all_of(all_covars))

#impute missing values (default: PMM and Multinomial Logit; does it 5 times, takes average)
imp_covars <- mice(covar_df, m=5, maxit = 1)

#enter into dataset, remove complete variables, and name as imputed variable (retain caseid)
test <- complete(imp_covars) %>% 
  dplyr::select(missing_covars) %>% 
  rename_at(vars(missing_covars), ~ paste0(.x ,"_imp"))

#recombine with real data 
final_drops <- cbind(final_drops, test)

#remove test / imp_covars
remove(test) 
remove(imp_covars)

```


## Moderators

```{r Media truthfullness}

final_drops <- final_drops %>% 
  mutate(
    truth_fox = coalesce(as.numeric(Q9), as.numeric(q9)), #Fox News 
    truth_fox = replace(truth_fox, truth_fox > 100, NA),
    truth_nyt = coalesce(as.numeric(Q10), as.numeric(q10)), #NYT
    truth_nyt = replace(truth_nyt, truth_nyt > 100, NA),
    truth_wsj = coalesce(as.numeric(Q11), as.numeric(q11)), #WSJ 
    truth_wsj = replace(truth_wsj, truth_wsj > 100, NA),
    truth_npr = coalesce(as.numeric(Q12), as.numeric(q12)), #NPR
    truth_npr = replace(truth_npr, truth_npr > 100, NA), 
    truth_soc = coalesce(as.numeric(Q13), as.numeric(q13)), #twitter/FB
    truth_soc = replace(truth_soc, truth_soc > 100, NA)
  )


table(is.na(final_drops$truth_soc))

#create index of average perception of truthfulness in all media 

#create scaled items
final_drops <- final_drops %>% 
  mutate(
    fox_scale = scale(truth_fox),
    nyt_scale = scale(truth_nyt),
    wsj_scale = scale(truth_wsj),
    npr_scale = scale(truth_npr),
    soc_scale = scale(truth_soc)
  )


#create keys 
keys_media <- list(
  final_drops=cs(fox_scale, nyt_scale, wsj_scale, npr_scale, soc_scale)
)

#alpha function 
tmp_media <- alpha(final_drops, keys_media, check.keys = T, use ="pairwise")

#create indexed variable (noting that fox is automatically reverse coded here)
final_drops <- final_drops %>% 
  mutate(
    truth_avg = tmp_media$scores
  )

#remove vestige
remove(tmp_media)
remove(keys_media)

#NYT/NPR vs. Fox News/WSJ
final_drops <- final_drops %>% #perceived truth of conservative outlets - liberal outlets
  mutate(
    truth_diff = (truth_fox+truth_wsj)
    -(truth_nyt+truth_npr),
    media_truth_idx = scale(truth_diff),
    media_truth_dum = case_when(
      media_truth_idx < median(media_truth_idx, na.rm=T) ~ 0, #less than median (liberal)
      media_truth_idx >= median(media_truth_idx, na.rm=T) ~ 1 #more than/equal median (conservative)
    )
  )

#separate 
final_drops <- final_drops %>% #perceived truth of conservative outlets - democratic outlets
  mutate(
    media_cons = scale(truth_fox+truth_wsj)
,
    media_libs = scale(truth_nyt+truth_npr)
  )


```


```{r Identity fusion}

#Identity fusion (A is farthest, G is closest)
final_drops <- final_drops %>% 
  mutate(
    ID_fusion = 
      case_when(
        Q8 == "A" ~ 0,
        Q8 == "B" ~ 1,
        Q8 == "C" ~ 2,
        Q8 == "D" ~ 3,
        Q8 == "E" ~ 4,
        Q8 == "F" ~ 5,
        Q8 == "G" ~ 6,
        Q8 == "1" ~ 0,
        Q8 == "2" ~ 1,
        Q8 == "3" ~ 2,
        Q8 == "4" ~ 3,
        Q8 == "5" ~ 4,
        Q8 == "6" ~ 5,
        Q8 == "7" ~ 6
      ),
    ID_fusion = replace(ID_fusion, ID_fusion > 6, NA)
  )

table(is.na(final_drops$ID_fusion))

```


```{r Partisan strength}

#Partisan strength
final_drops <- final_drops %>% 
  mutate(
    party_str = 
      case_when(
        #Q7 == "not asked" ~ 0, #folks who chose ind/other
        Q7 == "Very weak" ~ 1,
        Q7 == "Weak" ~ 2,
        Q7 == "Neutral" ~ 3,
        Q7 == "Strong" ~ 4,
        Q7 == "Very strong" ~ 5,
        #Q7 == "9" ~ 0, #folks who chose ind/other
        Q7 == "5" ~ 1,
        Q7 == "4" ~ 2,
        Q7 == "3" ~ 3,
        Q7 == "2" ~ 4,
        Q7 == "1" ~ 5
      )
  )

#This is not really usable since almost a 1000 participants were not asked partisan strength.

```


Not sure how to handle the "not sure" category here. 

```{r ideology}

#ideology (excluding not sure). Ideology_yg variable includes not sure as 0. 
final_drops <- final_drops %>% 
  mutate(
    ideo = #ideology
      case_when(
        Q4 == "Very liberal" ~ 1,
        Q4 == "Liberal" ~ 2,
        Q4 == "Moderate" ~ 3,
        Q4 == "Conservative" ~ 4,
        Q4 == "Very conservative" ~5,
        Q4 == "1" ~ 1,
        Q4 == "2" ~ 2,
        Q4 == "3" ~ 3,
        Q4 == "4" ~ 4,
        Q4 == "5" ~ 5,
      ),
    ideo_scale = scale(ideo)
  )

table(is.na(final_drops$ideo))

```


```{r Pol engagement}

#How closely do you follow the news about govt and politics
final_drops <- final_drops %>% 
  mutate(
    engage_news = 
      case_when(
        Q1 == "Not at all" ~ 0, 
        Q1 == "Not very closely" ~ 1,
        Q1 == "Somewhat closely" ~ 2,
        Q1 == "Very closely" ~ 3,
        Q1 == "4" ~ 0, 
        Q1 == "3" ~ 1,
        Q1 == "2" ~ 2,
        Q1 == "1" ~ 3
      )
  )

#Engagement in personal life (how often do you discuss govt or politics)
final_drops <- final_drops %>% 
  mutate(
    engage_person = 
      case_when(
        Q2 == "Less than monthly" ~ 0, 
        Q2 == "A few times a month" ~ 1,
        Q2 == "A few times a week" ~ 2,
        Q2 == "Nearly everyday" ~ 3,
        Q2 == "4" ~ 0, 
        Q2 == "3" ~ 1,
        Q2 == "2" ~ 2,
        Q2 == "1" ~ 3
      )
  )

#engage total
final_drops <- final_drops %>% 
  mutate(
    engage_total = 
      scale(engage_news+engage_person)
  )

table(is.na(final_drops$engage_total))

```


## Time stamps
```{r time stamps}

table(is.na(final_drops$starttime))

#get dates
final_drops <- final_drops %>% 
  mutate(
    start_w1 = as.POSIXlt(starttime,tz="UTC", format = "%Y-%m-%d %H:%M:%OS"),
    start_w2_sub = gsub("T", " ", starttime_w2), #replace T with space
    start_w2_corr = substr(start_w2_sub, 0,19), #remove seconds
    start_w2 = as.POSIXlt.character(start_w2_corr, tz="UTC", format = "%Y-%m-%d %H:%M:%OS"),
    start_w3_sub = gsub("T", " ", starttime_w3),
    start_w3_corr = substr(start_w3_sub, 0, 19), #extract only the dates
    start_w3 = as.POSIXlt.character(start_w3_corr, tz="UTC", format = "%Y-%m-%d %H:%M:%OS"),
    end_w1 = as.POSIXlt(endtime, tz="UTC", format = "%Y-%m-%d %H:%M:%OS"),
    end_w2_sub = gsub("T", " ", endtime_w2), #replace T with space
    end_w2_corr = substr(end_w2_sub, 0,19), #remove seconds
    end_w2 = as.POSIXlt.character(end_w2_corr, tz="UTC", format = "%Y-%m-%d %H:%M:%OS"),
    end_w3_sub = gsub("T", " ", endtime_w3), #replace T with space
    end_w3_corr = substr(end_w3_sub, 0,19), #remove seconds
    end_w3 = as.POSIXlt.character(end_w3_corr, tz="UTC", format = "%Y-%m-%d %H:%M:%OS"),
  )

#Create variable of time post-treatment 
final_drops <- final_drops %>% 
  mutate( 
    post_treat_w2 = as.numeric(difftime(final_drops$start_w2,final_drops$start_w1, units = "hours")),
    post_treat_w3 = as.numeric(difftime(final_drops$start_w3,final_drops$start_w1, units = "days")),
    post_treat_w1_w2 = as.numeric(difftime(final_drops$end_w2,final_drops$end_w1, units = "hours")),
    post_treat_w1_w3 = as.numeric(difftime(final_drops$end_w3,final_drops$end_w1, units = "days")),
    post_treat_w2_w3 = as.numeric(difftime(final_drops$end_w3,final_drops$end_w2, units = "days")),
    w3_sent = as.numeric(difftime(final_drops$start_w3,final_drops$end_w2, units = "days")),
    post_treat_w2_start = as.numeric(difftime(final_drops$start_w2,final_drops$end_w1, units = "hours")) 
)



```



## Attrition dummies

### Attrition dummies v1 (affective polarization index only)
```{r attrition dummies}

# dummy for attriter who is randomized (WITH bad cases)
final_drops <- final_drops %>% 
  mutate(
    rand_attriter = if_else(is.na(aff_pol_idx), 1, 0), #dummy for w2 attriters
    rand_attriter_w3 = if_else(is.na(aff_pol_idx_w3), 1, 0), #dummy for w3 attriters
    matched_attriter_w1 = if_else(is.na(weight),1, 0), #matched w1 attriter
    matched_attriter_w2 = #matched w2 attriter
      if_else(is.na(weight_w2),1, 0) |
      if_else(is.na(aff_pol_idx), 1, 0),
    matched_attriter_w3 = #matched w3 attriter
      if_else(is.na(weight_w3),1, 0) |
      if_else(is.na(aff_pol_idx_w3), 1, 0)
  )

#dummy for matched down attriters across waves (not accounting for overlap)
final_drops <- final_drops %>% 
  mutate(
    matchdown_attriters = 
      case_when(
        matched_attriter_w1 == 1 ~ 1,
        matched_attriter_w2 == 1 ~ 1,
        matched_attriter_w3 == 1 ~ 1
      ), 
    attriter_both = 
      if_else(rand_attriter ==1,1,0) & 
      if_else(rand_attriter_w3 ==1,1,0)
      )
  

```


## Inverse Probability Weighting

Covariates to be used listed below. Fewer than 50 missing values. 

* **Party_ID** = Republican (1) or Democrat (0)
* **Ideology_yg** = ideology, very liberal (1) to very conservative (5)
* **age** = participant age 
* **sex** = male (0) or female (1)
* **white** = dummy for whether participant identifies as white or not 
* **educ_dum** = dummy for whether participant has at least 4-year college degree or not 
* **marr_dum** = dummy for whether participant is married/in domestic partnership or not
* **christian** = dummy for whether participant identifies as Christian 
* **child** = dummy for whether participant has at least one child at home
* **turnout_2020** = dummy for whether participant voted in 2020 election
* **job** = dummy for employment status of participant (1= full or part-time employed)
* **region_2** = region where participant is from 


```{r IPW generation}

####Reverse Code Attrition Dummy####
final_drops <- final_drops %>% 
  mutate(
    resp_w2 = if_else(rand_attriter==1, 0, 1),
    resp_w3 = if_else(rand_attriter_w3==1, 0, 1),
    resp_matched_w2 = if_else(matched_attriter_w2==1, 0, 1),
    resp_matched_w3 = if_else(matched_attriter_w3==1, 0, 1)
  )


####IMPUTED COVARIATES####

####Wave 2 Full####

#regress attrition dummy on pretreatment covariates
interactions <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian_imp*treat_all", "child_imp*treat_all", "job_imp*treat_all", "marr_dum_imp*treat_all", "turnout2020_imp*treat_all", sep = "+")

#paste specification (response variable)
test_int1 <- paste("resp_w2 ~ ", interactions, sep = "")

#regress Logit
ipw_1 <- glm(test_int1, family = binomial(link = "logit"), final_drops)

#add inverted fitted values as weights
final_drops <- final_drops %>% 
  mutate(
    ipw_full_w2 = 1/(ipw_1$fitted)
)

table(final_drops$treat_all)

####Wave 3 Full####

#regress attrition dummy on pretreatment covariates
interactions <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian_imp*treat_all", "child_imp*treat_all", "job_imp*treat_all", "marr_dum_imp*treat_all", "turnout2020_imp*treat_all", sep = "+")

#paste specification
test_int2 <- paste("resp_w3 ~ ", interactions, sep = "")

#regress Logit
ipw_2 <- glm(test_int2, family = binomial(link = "logit"), final_drops)

#add inverted fitted values as weights
final_drops <- final_drops %>% 
  mutate(
    ipw_full_w3 = 1/(ipw_2$fitted)
)

####Wave 2 Nationally Representative####

#regress attrition dummy on pretreatment covariates
interactions <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian_imp*treat_all", "child_imp*treat_all", "job_imp*treat_all", "marr_dum_imp*treat_all", "turnout2020_imp*treat_all", sep = "+")

#paste specification
test_int3 <- paste("resp_matched_w2 ~ ", interactions, sep = "")

#regress Logit
ipw_3 <- glm(test_int3, family = binomial(link = "logit"), final_drops)

#add inverted fitted values as weights
final_drops <- final_drops %>% 
  mutate(
    ipw_matched_w2 = (weight_w2)*(1/(ipw_3$fitted))
)

####Wave 3 Nationally Representative####
interactions <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian_imp*treat_all", "child_imp*treat_all", "job_imp*treat_all", "marr_dum_imp*treat_all", "turnout2020_imp*treat_all", sep = "+")

#paste specification
test_int4 <- paste("resp_matched_w3 ~ ", interactions, sep = "")

#regress Logit
ipw_4 <- glm(test_int4, family = binomial(link = "logit"), final_drops)

#add inverted fitted values as weights
final_drops <- final_drops %>% 
  mutate(
    ipw_matched_w3 = (weight_w3)*(1/(ipw_4$fitted))
)

####NOT IMPUTED COVARIATES####

####Wave 2 Full####

#regress attrition dummy on pretreatment covariates
interactions1 <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian*treat_all", "child*treat_all", "job*treat_all", "marr_dum*treat_all", "turnout2020*treat_all", sep = "+")

#paste specification
test_int5 <- paste("resp_w2 ~ ", interactions1, sep = "")

#regress Logit
ipw_5 <- glm(test_int5, family = binomial(link = "logit"), final_drops)

####Wave 3 Full####

#regress attrition dummy on pretreatment covariates
interactions1 <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian*treat_all", "child*treat_all", "job*treat_all", "marr_dum*treat_all", "turnout2020*treat_all", sep = "+")

#paste specification
test_int6 <- paste("resp_w3 ~ ", interactions1, sep = "")

#regress Logit
ipw_6 <- glm(test_int6, family = binomial(link = "logit"), final_drops)


####Wave 2 Nationally Representative####

#regress attrition dummy on pretreatment covariates
interactions1 <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian*treat_all", "child*treat_all", "job*treat_all", "marr_dum*treat_all", "turnout2020*treat_all", sep = "+")

#interactions1 <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "child*treat_all", "job*treat_all", "marr_dum*treat_all", "turnout2020*treat_all", sep = "+") #RB's temporary code


#paste specification
test_int7 <- paste("resp_matched_w2 ~ ", interactions1, sep = "")

#regress
ipw_7 <- glm(test_int7, family=binomial(link="logit"),final_drops)

####Wave 3 Nationally Representative####
interactions1 <-  paste("treat_all*partyID", "ideology_yg*treat_all", "age*treat_all", "sex*treat_all", "educ_dum*treat_all", "white*treat_all", "region_2*treat_all", "christian*treat_all", "child*treat_all", "job*treat_all", "marr_dum*treat_all", "turnout2020*treat_all", sep = "+")

#paste specification
test_int8 <- paste("resp_matched_w3 ~ ", interactions1, sep = "")

#regress
ipw_8 <- glm(test_int8, family=binomial(link="logit"),final_drops)


#add inverted fitted values as weights
tmp_df <- final_drops %>% 
  filter(missers_any==0) %>% 
  mutate(
    ipw_full_w2_noimp = 1/(ipw_5$fitted.values),
    ipw_full_w3_noimp = 1/(ipw_6$fitted.values),
    ipw_matched_w2_noimp = (weight_w2)*(1/(ipw_7$fitted.values)),
    ipw_matched_w3_noimp = (weight_w3)*(1/(ipw_8$fitted.values))
    ) %>% 
  dplyr::select(caseid, ipw_full_w2_noimp, ipw_full_w3_noimp, 
         ipw_matched_w2_noimp, ipw_matched_w3_noimp)

#remerge with full dataset 
final_drops <- left_join(final_drops, tmp_df, by="caseid")

#rempve tmp_df
remove(tmp_df)


####Check Correlations between Imputed and Not Imputed####

#check correlations between imputed and non-imputed
cor(final_drops$ipw_full_w2, final_drops$ipw_full_w2_noimp, use="complete.obs") 
cor(final_drops$ipw_matched_w2, final_drops$ipw_matched_w2_noimp, use="complete.obs") 
cor(final_drops$ipw_full_w3, final_drops$ipw_full_w3_noimp, use="complete.obs") 
cor(final_drops$ipw_matched_w3, final_drops$ipw_matched_w3_noimp, use="complete.obs") 

#looks right


```


#### IPW based on Placebo vs. Long

```{r subsample}

long_placebo <- final_drops %>% 
  filter(!is.na(treat_long_collapse2))

long_placebo_2 <- long_placebo %>% 
  filter(!is.na(ipw_full_w2_noimp))

```


##### Non-matched 

```{r Creating full bivariate IPWs}
#regress attrition dummy on pretreatment covariates
interactions <-  paste("treat_long_collapse2*partyID", "ideology_yg*treat_long_collapse2", "age*treat_long_collapse2", "sex*treat_long_collapse2", "educ_dum*treat_long_collapse2", "white*treat_long_collapse2", "region_2*treat_long_collapse2", "christian_imp*treat_long_collapse2", "child_imp*treat_long_collapse2", "job_imp*treat_long_collapse2", "marr_dum_imp*treat_long_collapse2", "turnout2020_imp*treat_long_collapse2", sep = "+")

#paste specification
test_int1 <- paste("resp_w2 ~ ", interactions, sep = "")

#regress logit
ipw_1 <- glm(test_int1, family=binomial(link="logit"), long_placebo)

#add inverted fitted values as weights
long_placebo <- long_placebo %>% 
  mutate(
    ipw_full_w2_v2 = 1/(ipw_1$fitted)
)

#check
summary(long_placebo$ipw_full_w2)
summary(long_placebo$ipw_full_w2_v2)

#wave 3

#regress attrition dummy on pretreatment covariates
interactions <-  paste("treat_long_collapse2*partyID", "ideology_yg*treat_long_collapse2", "age*treat_long_collapse2", "sex*treat_long_collapse2", "educ_dum*treat_long_collapse2", "white*treat_long_collapse2", "region_2*treat_long_collapse2", "christian_imp*treat_long_collapse2", "child_imp*treat_long_collapse2", "job_imp*treat_long_collapse2", "marr_dum_imp*treat_long_collapse2", "turnout2020_imp*treat_long_collapse2", sep = "+")

#paste specification
test_int2 <- paste("resp_w3 ~ ", interactions, sep = "")

#regress logit
ipw_2 <- glm(test_int2,family=binomial(link="logit"), long_placebo)

#add inverted fitted values as weights
long_placebo <- long_placebo %>% 
  mutate(
    ipw_full_w3_v2 = 1/(ipw_2$fitted)
)

#check
summary(long_placebo$ipw_full_w3)
summary(long_placebo$ipw_full_w3_v2)

#### No Imputation ####

#wave 2
interactions1 <-  paste("treat_long_collapse2*partyID", "ideology_yg*treat_long_collapse2", "age*treat_long_collapse2", "sex*treat_long_collapse2", "educ_dum*treat_long_collapse2", "white*treat_long_collapse2", "region_2*treat_long_collapse2", "christian*treat_long_collapse2", "child*treat_long_collapse2", "job*treat_long_collapse2", "marr_dum*treat_long_collapse2", "turnout2020*treat_long_collapse2", sep = "+")

#paste specification
test_int3 <- paste("resp_w2 ~ ", interactions1, sep = "")

#regress glm
ipw_3 <- glm(test_int3, family=binomial(link="logit"), long_placebo)

#wave 3
#paste specification
test_int4 <- paste("resp_w3 ~ ", interactions1, sep = "")

#regress logit
ipw_4 <- glm(test_int4, family=binomial(link="logit"), long_placebo)


#add inverted fitted values as weights
tmp_df <- long_placebo_2 %>% 
  mutate(
    ipw_full_w2_noimp_v2 = 1/(ipw_3$fitted),
    ipw_full_w3_noimp_v2 = 1/(ipw_4$fitted)
) %>% 
  dplyr::select(caseid, ipw_full_w2_noimp_v2, ipw_full_w3_noimp_v2)

#remerge 
long_placebo <- left_join(long_placebo, tmp_df, by="caseid")

tmp_df2 <- long_placebo %>% 
  dplyr::select(caseid, ipw_full_w2_v2, ipw_full_w3_v2, ipw_full_w2_noimp_v2, ipw_full_w3_noimp_v2)

#merge
final_drops <- left_join(final_drops, tmp_df2, by="caseid")

#correlations: looks good
cor(final_drops$ipw_full_w3_v2, final_drops$ipw_full_w3_noimp_v2, use = "complete.obs")
cor(final_drops$ipw_full_w2_v2, final_drops$ipw_full_w2_noimp_v2, use = "complete.obs")


```


##### Matched sample 

```{r Creating matched bivariate IPWs}
#regress attrition dummy on pretreatment covariates
interactions <-  paste("treat_long_collapse2*partyID", "ideology_yg*treat_long_collapse2", "age*treat_long_collapse2", "sex*treat_long_collapse2", "educ_dum*treat_long_collapse2", "white*treat_long_collapse2", "region_2*treat_long_collapse2", "christian_imp*treat_long_collapse2", "child_imp*treat_long_collapse2", "job_imp*treat_long_collapse2", "marr_dum_imp*treat_long_collapse2", "turnout2020_imp*treat_long_collapse2", sep = "+")

#paste specification
test_int1 <- paste("resp_matched_w2 ~ ", interactions, sep = "")

#regress logit
ipw_1 <- glm(test_int1, family=binomial(link = "logit"), long_placebo)

#add inverted fitted values as weights
long_placebo <- long_placebo %>% 
  mutate(
    ipw_matched_w2_v2 = (weight_w2)*(1/(ipw_1$fitted))
)


#wave 3

#regress attrition dummy on pretreatment covariates
interactions <-  paste("treat_long_collapse2*partyID", "ideology_yg*treat_long_collapse2", "age*treat_long_collapse2", "sex*treat_long_collapse2", "educ_dum*treat_long_collapse2", "white*treat_long_collapse2", "region_2*treat_long_collapse2", "christian_imp*treat_long_collapse2", "child_imp*treat_long_collapse2", "job_imp*treat_long_collapse2", "marr_dum_imp*treat_long_collapse2", "turnout2020_imp*treat_long_collapse2", sep = "+")

#paste specification
test_int2 <- paste("resp_matched_w3 ~ ", interactions, sep = "")

#regress logit
ipw_2 <- glm(test_int2, family=binomial(link="logit"), long_placebo)

#add inverted fitted values as weights
long_placebo <- long_placebo %>% 
  mutate(
    ipw_matched_w3_v2 = (weight_w3)*(1/(ipw_2$fitted))
)


#### No Imputation ####

#wave 2
interactions1 <-  paste("treat_long_collapse2*partyID", "ideology_yg*treat_long_collapse2", "age*treat_long_collapse2", "sex*treat_long_collapse2", "educ_dum*treat_long_collapse2", "white*treat_long_collapse2", "region_2*treat_long_collapse2", "christian*treat_long_collapse2", "child*treat_long_collapse2", "job*treat_long_collapse2", "marr_dum*treat_long_collapse2", "turnout2020*treat_long_collapse2", sep = "+")

#paste specification
test_int3 <- paste("resp_matched_w2 ~ ", interactions1, sep = "")

#regress logit
ipw_3 <- glm(test_int3, family=binomial(link="logit"), long_placebo)

#wave 3
#paste specification
test_int4 <- paste("resp_matched_w3 ~ ", interactions1, sep = "")

#regress logit
ipw_4 <- glm(test_int4, family=binomial(link="logit"), long_placebo)


#add inverted fitted values as weights
tmp_df <- long_placebo_2 %>% 
  mutate(
    ipw_matched_w2_noimp_v2 = (weight_w2)*(1/(ipw_3$fitted)),
    ipw_matched_w3_noimp_v2 = (weight_w3)*(1/(ipw_4$fitted))
) %>% 
  dplyr::select(caseid, ipw_matched_w2_noimp_v2, ipw_matched_w3_noimp_v2)

#remerge 
long_placebo <- left_join(long_placebo, tmp_df, by="caseid")


tmp_df2 <- long_placebo %>% 
  dplyr::select(caseid, ipw_matched_w2_v2, ipw_matched_w3_v2, ipw_matched_w2_noimp_v2, ipw_matched_w3_noimp_v2)

#merge
final_drops <- left_join(final_drops, tmp_df2, by="caseid")

#correlations: looks good
cor(final_drops$ipw_matched_w3_v2, final_drops$ipw_matched_w3_noimp_v2, use = "complete.obs")
cor(final_drops$ipw_matched_w2_v2, final_drops$ipw_matched_w2_noimp_v2, use = "complete.obs")

```

## Attention checks

Manipulation check 1 (VC and full video):
In the video, two of the participants, Kuwar and Greg, become fast friends. At the
conclusion of the workshop, they decide to spend time together in each other’s hometowns.
Where do they meet?

Correct answer: At their church and mosque respectively. However, at their favorite restaurant could also be plausibly correct while reflecting less careful attention (they share a meal at a restaurant but don't talk about the restaurant as their favorite) 


Manipulation check 2 (PMC and full video): 
In the video, the workshop participants describe stereotypes about each political
party. Which of the following was NOT discussed?

Correct answer is food preferences (e.g. fast food)


Placebo check:In the video, which migration of animals is the primary focus of the video?

Correct answer is wildebeest 

```{r attention checks}

final_drops <- final_drops %>% 
  mutate(
    compliance1 = case_when(
      Q22 == "At their mosque and church, respectively" ~ 1, #correct 
      Q22 == "At the local movie theater" ~ 0,
      Q22 == "At the local shopping mall" ~ 0,
      Q22 == "At their favorite restaurants" ~ 0,
      Q22 == "skipped" ~ 0,
      Q22 == "Not asked" ~ NA_real_,
      Q22 == "2" ~ 1, #correct 
      Q22 == "1" ~ 0,
      Q22 == "3" ~ 0,
      Q22 == "4" ~ 0, 
      Q22 == "8" ~ 0,
      Q22 == "9" ~ NA_real_
    ),
    
    compliance2 = case_when(
      Q23 == "Food preferences (e.g., fast food)" ~ 1, #correct 
      Q23 == "Intolerance of others (e.g., immigrants)" ~ 0, 
      Q23 == "Policy preferences (e.g., raising the minimum wage)" ~ 0, 
      Q23 == "skipped" ~ 0, 
      Q23 == "not asked" ~ NA_real_,
      Q23 == "2" ~ 1, #correct 
      Q23 == "1" ~ 0, 
      Q23 == "3" ~ 0,
      Q23 == "8" ~ 0,
      Q23 == "9" ~ NA_real_
    ),
    
    compliance1b = case_when(
      Q22 == "At their mosque and church, respectively" ~ 1, #correct 
      Q22 == "At the local movie theater" ~ 0,
      Q22 == "At the local shopping mall" ~ 0,
      Q22 == "At their favorite restaurants" ~ 1, #also plausibly correct
      Q22 == "skipped" ~ 0,
      Q22 == "Not asked" ~ NA_real_,
      Q22 == "2" ~ 1, #correct 
      Q22 == "1" ~ 1, #also plausibly correct 
      Q22 == "3" ~ 0,
      Q22 == "4" ~ 0, 
      Q22 == "8" ~ 0,
      Q22 == "9" ~ NA_real_
    ),
    
    compliance_placebo = case_when(
      Q24 == "Wildebeest" ~ 1, #correct
      Q24 == "Geese" ~ 0,
      Q24 == "Monarch butterflies" ~ 0, 
      Q24 == "Gray whales" ~ 0,
      Q24 == "not asked" ~ NA_real_,
      Q24 == "1" ~ 1, #correct
      Q24 == "2" ~ 0, 
      Q24 == "3" ~ 0,
      Q24 == "4" ~ 0,
      Q24 == "9" ~ NA_real_
    )
    )

summary(final_drops$compliance1) #70% compliance
summary(final_drops$compliance1b) #86% compliance 
summary(final_drops$compliance2) #82% compliance 
summary(final_drops$compliance_placebo) #85% compliance 


#create indicators 
final_drops <- final_drops %>%
  mutate(
    minimal_compliance = case_when(
      is.na(compliance1) | is.na(compliance2) | is.na(compliance1b) ~ NA_real_,
      compliance1 == 1 | compliance2 == 1 | compliance1b == 1 ~ 1,
      TRUE ~ 0
    ),
    medium_compliance = case_when(
      is.na(compliance1) | is.na(compliance2) ~ NA_real_,
      compliance1 == 1 | compliance2 == 1 ~ 1,
      TRUE ~ 0
    ),
    maximum_compliance = case_when(
      is.na(compliance1) | is.na(compliance2) ~ NA_real_,
      compliance1 == 1 & compliance2 == 1 ~ 1,
      TRUE ~ 0
    )
  )

summary(final_drops$medium_compliance)

```


# Export Dataset Clean Dataset

```{r export clean dataset}

####Modify working directory #####
write.csv(final_drops, file = "/Users/LAK/Brown Dropbox/Lee-Or Ankori-Karlinsky/Braver Angels Documentary/PSRM_ReplicationPackage/ContentContact_ReplicationPackage/Data/Cleaned/final_drops.csv")

```
